--DROP TABLE TBMON_PROFILE_PARAM;
--DROP TABLE TBMON_PROFILE;
CREATE TABLE TBMON_PROFILE
(
	GUID			INTEGER			NOT NULL	IDENTITY (1, 1),
	TITLE			VARCHAR(250)	NOT NULL,
	CAPTION			VARCHAR(250),
	EXEC_SQL		NVARCHAR(MAX),
	RETURN_TYPE		VARCHAR(100)	NOT NULL,	--TreeView or Table
	COMMENT			VARCHAR(500),
	ACTIVE			BIT				NOT NULL	DEFAULT 1,
	ADDED_WHO	    VARCHAR(100)    NOT NULL    DEFAULT 'System',
	ADDED_WHEN   	DATETIME 	    NOT NULL 	DEFAULT (GETDATE()),
	CHANGED_WHO     VARCHAR(100),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT PK_TBMON_PROFILE 	   PRIMARY KEY (GUID),
	--CONSTRAINT FK_OS_CAT_ID FOREIGN KEY (CAT_ID)  REFERENCES  TBIDB_CATALOG (GUID)	,
	CONSTRAINT UQ1_TBMON_PROFILE	   UNIQUE (TITLE),
)
GO
CREATE TRIGGER TBIDB_OBJECT_STORE_AFT_UPD ON TBMON_PROFILE
FOR UPDATE
AS
  UPDATE TBMON_PROFILE SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBMON_PROFILE.GUID = INSERTED.GUID
GO

CREATE TABLE TBMON_PROFILE_PARAM
(
	GUID			INTEGER			NOT NULL	IDENTITY (1, 1),
	PROFILE_ID		INTEGER			NOT NULL,
	CAPTION			VARCHAR(250)	NOT NULL,
	P_TYPE			VARCHAR(50)		NOT NULL	DEFAULT 'TEXT',
	ITEMS			NVARCHAR(MAX),
	SEQUENCE		TINYINT			NOT NULL	DEFAULT 0,
	OPTIONAL		BIT				NOT NULL	DEFAULT 0,
	COMMENT			VARCHAR(500),
	ACTIVE			BIT				NOT NULL	DEFAULT 1,
	ADDED_WHO	    VARCHAR(100)    NOT NULL    DEFAULT 'System',
	ADDED_WHEN   	DATETIME 	    NOT NULL 	DEFAULT (GETDATE()),
	CHANGED_WHO     VARCHAR(100),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT PK_TBMON_PROFILE_PARAM 	   PRIMARY KEY (GUID),
	CONSTRAINT FK_TBMON_PROFILE_PARAM_OID FOREIGN KEY (PROFILE_ID)  REFERENCES  TBMON_PROFILE (GUID)	,
	CONSTRAINT UQ1_TBMON_PROFILE_PARAM	   UNIQUE (CAPTION),
)
GO
CREATE TRIGGER TBMON_PROFILE_PARAM_AFT_UPD ON TBMON_PROFILE_PARAM
FOR UPDATE
AS
  UPDATE TBMON_PROFILE_PARAM SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBMON_PROFILE_PARAM.GUID = INSERTED.GUID
GO