views:

201

answers:

1

Hi there, can't seem to find a way to do this anywhere. I have an SQL trigger that fires on an update. The trigger copies some details about the update to another table that someone elses program uses. Among the details passed is the name of the updated column. There a many, many, potential columns that have been updated (although only one at any one time), and I could just use something like:

IF UPDATE(ThisColumn)
BEGIN
   Do stuff with ThisColumn
END
IF UPDATE(ThatColumn)
BEGIN
   Do same stuff with ThatColumn
END

But as I said there are many columns in this table and I'm sure there must be an elgant way to get the name of the column that has been updated without me typing pages of essentially the same IF statements.

Thanks in advance!

+1  A: 

You can use COLUMNS_UPDATED

Example usage 1, database journal

Example usage 2, KB 232195 The number derived here maps to sys.columns.column_id

I used to have working examples, but not any more

Huge gotcha: Because of metadata visibility, the join to sys.columns may fail. This is especially important where teh call is wrapped in a stored proc. You have to use "EXECUTE AS OWNER" in the trigger to

gbn