Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Fetch Upated Column Name using Triggers

Author  Topic 

ravininave
Posting Yak Master

111 Posts

Posted - 2013-02-17 : 19:45:18
I'm writing a trigger for DELETE,UPDATE. Actually I've to create a log in a table about updated fields with old and new data. In my table there are at least 30-40 fields. I've to fetch only updated field Name with old data and new data and store it like

AppID, FieldName, OldValue, NewValue

(Current I'm checking on each field with old and new value and Inserting in log table)
Can anyone share some piece of code.?
Thanx

VB6/ASP.NET
------------------------
http://www.nehasoftec.com

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-17 : 20:32:23
You should be able to use the COLUMNS_UPDATED property from within the trigger. However, personally, I have not used it for couple of reasons. Seems to require somewhat complex logic (at least to my simple-minded way of thinking) that involves bit-patterns, the number of columns you have in the table etc. If you are interested, documentation and examples are here:
http://msdn.microsoft.com/en-us/library/ms186329.aspx

Instead, I have always simply saved the old values (and/or the new values) in a table that has the same structure as the original table (plus possibly some additional auditing information columns). Doing it the way you are doing makes it easier to query for changes in some cases, but in some other cases, the simple-minded approach works well. And, you lose type-safety when you save auditing information the way you are storing it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-17 : 23:24:06
you can also use Change Data Capture functionality for this

http://blog.sqlauthority.com/2009/08/15/sql-server-introduction-to-change-data-capture-cdc-in-sql-server-2008/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -