Question : help with trigger/stored procedure

Hello experts,
When executing the attached code (trigger created via stored procedure) like so EXECUTE GenerateAudittrail 'Records_Retention' , I get error;-

Msg 102, Level 15, State 1, Procedure tr_Records_Retention_Update, Line 1
Incorrect syntax near '@FldsUpdated'.
Msg 137, Level 15, State 1, Procedure tr_Records_Retention_Update, Line 1
Must declare the scalar variable "@ColumnsUpdated".
Msg 137, Level 15, State 2, Procedure tr_Records_Retention_Update, Line 1
Must declare the scalar variable "@ColumnsUpdated".

I have tried in vain to figure out why the error is occuring. Need a little help pls..

Merci..
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
-- Declare variable to build statements for UPDATE trigger
	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 + 'DECLARE @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'

Answer : help with trigger/stored procedure

Here you go
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
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
Random Solutions  
 
programming4us programming4us