I had a question in relation to this topic from here Log record changes in SQL server in an audit table
Which helped me fine, but I had a follow up question to the answer that was provided. In the audit table. I am trying to capture one of the columns from the table being audited..let's call the column name 'ID'. I have accounted for this column in the auditing table, but can't figure out how to code it to where it will capture 'ID' column value and store it in the auditing table as well.
Can anyone help me out here?
Basically from 'TableBeingAudited' there is a column named 'ID'
I wanted the value from the 'ID' column captured and stored into the 'AuditTable'
I can't figure out how to code it. I know I have to declare it, but can't figure out how to code it into the insert/select part of the code here:
insert Audit ( Type,
TableName,
PK,
FieldName,
OldValue,
NewValue,
UpdateDate,
UserName)
select ''' + @Type + ''','''
+ @TableName + ''',' + @PKSelect
+ ',''' + @fieldname + ''''
+ ',convert(varchar(1000),d.' + @fieldname + ')'
+ ',convert(varchar(1000),i.' + @fieldname + ')'
+ ',''' + @UpdateDate + ''''
+ ',''' + @UserName + ''''
+ ' from #ins i full outer join #del d'
+ @PKCols
+ ' where i.' + @fieldname + ' <> d.' + @fieldname
+ ' or (i.' + @fieldname + ' is null and d.'
+ @fieldname
+ ' is not null)'
+ ' or (i.' + @fieldname + ' is not null and d.'
+ @fieldname
+ ' is null)'
EXEC (@sql)
END
END
GO