CREATE TABLE TBPMO_TEMPLATE_ENTITY
(
	GUID			INTEGER			NOT NULL	IDENTITY(1,1),
	TEMPLATE_ID		INTEGER			NOT NULL,
	ENTITY_ID		INTEGER			NOT NULL,
	ADDED_WHO		VARCHAR(50)					DEFAULT 'Digital Data',
	ADDED_WHEN		DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBPMO_TEMPLATE_ENTITY				PRIMARY KEY (GUID),
	CONSTRAINT		FK_TBPMO_TEMPLATE_ENTITY_TEMPLATE_ID	FOREIGN KEY(TEMPLATE_ID)	REFERENCES TBPMO_TEMPLATE(GUID),
	CONSTRAINT		FK_TBPMO_TEMPLATE_ENTITY_ENTITY_ID		FOREIGN KEY(ENTITY_ID)		REFERENCES TBPMO_FORM(GUID)
)
GO

CREATE TRIGGER TBPMO_TEMPLATE_ENTITY_AFT_UPD ON TBPMO_TEMPLATE_ENTITY
FOR UPDATE
AS
	UPDATE TBPMO_TEMPLATE_ENTITY SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_TEMPLATE_ENTITY.GUID = INSERTED.GUID
GO