USE <database, sysname, myProducts>
GO

/*
*/

-- drop table
IF OBJECT_ID('<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>', 'U') IS NOT NULL
  BEGIN
	DROP TABLE dbo.<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>
  END
GO

-- create table
CREATE TABLE dbo.<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>
	(
		<table_suffix, sysname, CoTbl>ID int IDENTITY(1,1) NOT NULL,
	--	<FK_table_suffix, sysname, CoTbl>ID INT NOT NULL,
		Nvar nvarchar(x) NOT NULL,
		Inte int NOT NULL,

		Db_InsDt smalldatetime NOT NULL CONSTRAINT DF_<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>_DbInsDt DEFAULT (getdate()),
		Db_InsUser nvarchar(50) NOT NULL CONSTRAINT DF_<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>_DbInsUser DEFAULT (right(suser_sname(),(50))),
		Db_UpdDt smalldatetime NULL,
		Db_UpdUser nvarchar(50) NULL,

		CONSTRAINT PK_<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl> PRIMARY KEY CLUSTERED(<table_suffix, sysname, CoTbl>ID ASC)
	)
GO

-- define update trigger
CREATE TRIGGER ut_<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl> ON <table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl> AFTER UPDATE
AS
  BEGIN
	UPDATE <table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>
	SET Db_UpdDt = getdate(),
		Db_UpdUser = right(suser_sname(), 50)
	FROM inserted i
	WHERE <table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>.<table_suffix, sysname, CoTbl>ID = i.<table_suffix, sysname, CoTbl>ID
  END
GO

-- set unique index on Code
CREATE UNIQUE NONCLUSTERED INDEX IX_<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>__Unique_Code ON <table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl> (<FK_table_suffix, sysname, CoTbl>ID)
GO

/*
-- set foreign-key constraints
ALTER TABLE <table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl> WITH CHECK ADD
	CONSTRAINT FK_<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>__<table_prefix, sysname, TBL>_<FK_table_suffix, sysname, CoTbl>
	FOREIGN KEY(<FK_table_suffix, sysname, CoTbl>ID)
REFERENCES <table_prefix, sysname, TBL>_<FK_table_suffix, sysname, CoTbl> (<FK_table_suffix, sysname, CoTbl>ID)

ALTER TABLE <table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>
	CHECK CONSTRAINT FK_<table_prefix, sysname, TBL>_<table_suffix, sysname, CoTbl>__<table_prefix, sysname, TBL>_<FK_table_suffix, sysname, CoTbl>
GO

*/
