DECLARE @CreateUpdateTrigger VARCHAR(8000)
SET @CreateUpdateTrigger = 'CREATE TRIGGER tr_' + @TableName + '_Update ON '
+ @Owner + '.' + @TableName
SET @CreateUpdateTrigger = @CreateUpdateTrigger + ' FOR UPDATE '
SET @CreateUpdateTrigger = @CreateUpdateTrigger + ' AS '
SET @CreateUpdateTrigger = @CreateUpdateTrigger + ' BEGIN '
SET @CreateUpdateTrigger = @CreateUpdateTrigger
+ ' DECLARE @FldsUpdated XML, '
SET @CreateUpdateTrigger = @CreateUpdateTrigger
+ ' @ColumnsUpdated VARBINARY(100) '
SET @CreateUpdateTrigger = @CreateUpdateTrigger
+ ' SET @ColumnsUpdated = COLUMNS_UPDATED() '
SET @CreateUpdateTrigger = @CreateUpdateTrigger
+ ' SET @FldsUpdated = (SELECT COLUMN_NAME AS Name FROM TRS.INFORMATION_SCHEMA.COLUMNS Field WHERE TABLE_NAME = '
+ @TableName + ' AND '
SET @CreateUpdateTrigger = @CreateUpdateTrigger
+ 'sys.fn_IsBitSetInBitmask (@ColumnsUpdated, COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + ''.'' + TABLE_NAME), COLUMN_NAME, ''ColumnID'')'
SET @CreateUpdateTrigger = @CreateUpdateTrigger
+ ') <> 0 FOR XML AUTO, ROOT(''Fields''))'
SET @CreateUpdateTrigger = @CreateUpdateTrigger + ' INSERT INTO ' + @TableName
+ @AuditNameExtention + ' (' + @ListOfFields + ', AuditAction) SELECT '
+ @ListOfFields + ', ''U'' FROM Inserted '
SET @CreateUpdateTrigger = @CreateUpdateTrigger + 'END'
SELECT @CreateUpdateTrigger
|