ALTER TABLE dbo.m_eform_document   DROP CONSTRAINT FK_m_eform_document_status
GO
ALTER TABLE dbo.m_eform_document   DROP CONSTRAINT FK_m_eform_document_eform
GO
ALTER TABLE dbo.m_eform_document   DROP CONSTRAINT FK_m_eform_document_document
GO
CREATE TABLE dbo.Tmp_m_eform_document   (   eform_document_id int NOT NULL IDENTITY (1, 1),   eform_id int NOT NULL,   document_id int NOT NULL,   minimum_files numeric(18, 0) NOT NULL,   maximum_files numeric(18, 0) NOT NULL,   applies_to char(1) NOT NULL,   seq_number numeric(18, 0) NOT NULL,   status_id int NOT NULL,   created_by varchar(8) NOT NULL,   created_date datetime NOT NULL,   modified_by varchar(8) NOT NULL,   modified_date datetime NOT NULL,   is_delete char(1) NOT NULL   )  ON [PRIMARY]  
GO
ALTER TABLE dbo.Tmp_m_eform_document SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.m_eform_document)
	 EXEC('INSERT INTO dbo.Tmp_m_eform_document (eform_document_id, eform_id, document_id, minimum_files, maximum_files, applies_to, seq_number, status_id, created_by, created_date, modified_by, modified_date, is_delete)
		SELECT eform_document_id, eform_id, document_id, minimum_files, maximum_files, ''a'', seq_number, status_id, created_by, created_date, modified_by, modified_date, is_delete FROM dbo.m_eform_document WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.Tmp_m_eform_document ADD CONSTRAINT   DF_m_eform_document_applies_to DEFAULT 'a' FOR applies_to
GO
DROP TABLE dbo.m_eform_document  
GO
EXECUTE sp_rename N'dbo.Tmp_m_eform_document', N'm_eform_document', 'OBJECT'   
GO
ALTER TABLE dbo.m_eform_document ADD CONSTRAINT   PK__m_eform___BF27DE2600EA0E6F PRIMARY KEY CLUSTERED    (   eform_document_id   ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
ALTER TABLE dbo.m_eform_document ADD CONSTRAINT   FK_m_eform_document_document FOREIGN KEY   (   document_id   ) REFERENCES dbo.m_document   (   document_id   ) ON UPDATE  NO ACTION     ON DELETE  NO ACTION
ALTER TABLE dbo.m_eform_document ADD CONSTRAINT   FK_m_eform_document_eform FOREIGN KEY   (   eform_id   ) REFERENCES dbo.m_eform   (   eform_id   ) ON UPDATE  NO ACTION     ON DELETE  NO ACTION
ALTER TABLE dbo.m_eform_document ADD CONSTRAINT   FK_m_eform_document_status FOREIGN KEY   (   status_id   ) REFERENCES dbo.m_status   (   status_id   ) ON UPDATE  NO ACTION     ON DELETE  NO ACTION
GO
CREATE TRIGGER [dbo].[m_eform_document_Update]        ON dbo.m_eform_document      FOR UPDATE      AS               DECLARE @UpdatedCols varchar(max)         SELECT @UpdatedCols =  CASE WHEN UPDATE([eform_document_id]) THEN '[eform_document_id]-' ELSE '' END +   CASE WHEN UPDATE([eform_id]) THEN '[eform_id]-' ELSE '' END +   CASE WHEN UPDATE([document_id]) THEN '[document_id]-' ELSE '' END +   CASE WHEN UPDATE([minimum_files]) THEN '[minimum_files]-' ELSE '' END +   CASE WHEN UPDATE([maximum_files]) THEN '[maximum_files]-' ELSE '' END +   CASE WHEN UPDATE([seq_number]) THEN '[seq_number]-' ELSE '' END +   CASE WHEN UPDATE([status_id]) THEN '[status_id]-' ELSE '' END +   CASE WHEN UPDATE([created_by]) THEN '[created_by]-' ELSE '' END +   CASE WHEN UPDATE([created_date]) THEN '[created_date]-' ELSE '' END +   CASE WHEN UPDATE([modified_by]) THEN '[modified_by]-' ELSE '' END +   CASE WHEN UPDATE([modified_date]) THEN '[modified_date]-' ELSE '' END +   CASE WHEN UPDATE([is_delete]) THEN '[is_delete]-' ELSE '' END             IF LTRIM(RTRIM(@UpdatedCols)) <> ''      BEGIN             INSERT INTO [dbo].[log_m_eform_document]  ([eform_document_id],[eform_id],[document_id],[minimum_files],[maximum_files],[seq_number],[status_id],[created_by],[created_date],[modified_by],[modified_date],[is_delete],AuditDataState,AuditDMLAction,AuditUser,AuditDateTime,UpdateColumns)  SELECT CONVERT(int,[eform_document_id]) as [eform_document_id],[eform_id],[document_id],[minimum_files],[maximum_files],[seq_number],[status_id],[created_by],[created_date],[modified_by],[modified_date],[is_delete],'New','Update',SUSER_SNAME(),getdate(),@UpdatedCols  FROM INSERTED                       INSERT INTO [dbo].[log_m_eform_document]  ([eform_document_id],[eform_id],[document_id],[minimum_files],[maximum_files],[seq_number],[status_id],[created_by],[created_date],[modified_by],[modified_date],[is_delete],AuditDataState,AuditDMLAction,AuditUser,AuditDateTime,UpdateColumns)  SELECT CONVERT(int,[eform_document_id]) as [eform_document_id],[eform_id],[document_id],[minimum_files],[maximum_files],[seq_number],[status_id],[created_by],[created_date],[modified_by],[modified_date],[is_delete],'Old','Update',SUSER_SNAME(),getdate(),@UpdatedCols  FROM DELETED       END  
CREATE TRIGGER [dbo].[m_eform_document_Insert]   ON dbo.m_eform_document      FOR INSERT      AS         INSERT INTO [dbo].[log_m_eform_document]  ([eform_document_id],[eform_id],[document_id],[minimum_files],[maximum_files],[seq_number],[status_id],[created_by],[created_date],[modified_by],[modified_date],[is_delete],AuditDataState,AuditDMLAction,AuditUser,AuditDateTime,UpdateColumns)  SELECT CONVERT(int,[eform_document_id]) as [eform_document_id],[eform_id],[document_id],[minimum_files],[maximum_files],[seq_number],[status_id],[created_by],[created_date],[modified_by],[modified_date],[is_delete],'New','Insert',SUSER_SNAME(),getdate(),''  FROM INSERTED  
CREATE TRIGGER [dbo].[m_eform_document_Delete]      ON dbo.m_eform_document      FOR DELETE      AS       INSERT INTO [dbo].[log_m_eform_document]  ([eform_document_id],[eform_id],[document_id],[minimum_files],[maximum_files],[seq_number],[status_id],[created_by],[created_date],[modified_by],[modified_date],[is_delete],AuditDataState,AuditDMLAction,AuditUser,AuditDateTime,UpdateColumns)  SELECT CONVERT(int,[eform_document_id]) as [eform_document_id],[eform_id],[document_id],[minimum_files],[maximum_files],[seq_number],[status_id],[created_by],[created_date],[modified_by],[modified_date],[is_delete],'Old','Delete',SUSER_SNAME(),getdate(),''  FROM DELETED  