SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Fetch Upated Column Name using Triggers
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

106 Posts

Posted - 02/17/2013 :  19:45:18  Show Profile  Reply with Quote
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.?


James K
Flowing Fount of Yak Knowledge

3865 Posts

Posted - 02/17/2013 :  20:32:23  Show Profile  Reply with Quote
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:

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.

Edited by - James K on 02/17/2013 20:36:31
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 02/17/2013 :  23:24:06  Show Profile  Reply with Quote
you can also use Change Data Capture functionality for this


SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000