Hiya,I've been trying to get if update()
to work when using a variable to pass the field name, but all I get is a incorrect syntax error.What I'm trying to do is insert into a table a list of fields that were updated, and there old and new values (you can see where I'm heading below). Yes, I'm using a cursor, I found more examples of them when I was learning this, and I've not had time to work out another way, yet.Feel free to convert it to none-cursor if you so wish :-)Any Help would be much appreciatedCREATE TRIGGER tr_tcd_journal ON [dbo].[t_ContractorDetails] for UPDATEASBEGIN declare @numFields int, @colName nvarchar(255), @tableName nvarchar(40), @value nvarchar(4000), @uidcol varchar(255), @bol bit set @tableName='t_ContractorDetails' set @bol=0 set @value='Bob' Declare cur_fields CURSOR For Select Column_Name from INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = @tableName) Open cur_fields FETCH NEXT FROM cur_fields Into @ColName While (@@Fetch_Status = 0 ) and (@bol=0) Begin select @bol=COLUMNPROPERTY( OBJECT_ID(@tableName),@ColName,'IsIdentity') if @bol=1 Begin set @uidcol=@ColName end FETCH NEXT FROM cur_fields Into @ColName end Close cur_fields open cur_fields FETCH NEXT FROM cur_fields Into @ColName While (@@Fetch_Status = 0 ) Begin IF UPDATE(ColName) begin INSERT INTO t_Journal (nv_Table, nv_Field,dt_DateTime, nv_OldValue, nv_NewValue) VALUES (@tableName,@ColName, GETDATE(), @uidcol,@value) end FETCH NEXT FROM cur_fields Into @ColName end Close cur_fields Deallocate cur_fieldsEND