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 2005 Forums
 Transact-SQL (2005)
 Trigger when Update some columns

Author  Topic 

bekeer020
Starting Member

24 Posts

Posted - 2012-01-01 : 05:39:48
Dear All

I want to creat trigger when some fields update i want after updated insert new row in another table (Old value,new Value)
for example mark1,mark2 columns

i want result in another table like this:
mark1olde,mark1new,mark2old,mark2new columns


can any one help me please

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-01 : 12:07:08
You can compare the values from the INSERTED virtual table against the DELETED virtual table to see if specific values have been updated, and then make decision whether to insert data into your audit table based on the result of that test. The UPDATE function (http://msdn.microsoft.com/en-us/library/ms187326.aspx) may be useful to you in this context.

One thing that tripped me up when I was working with triggers for the first time was that, the trigger will be called only once per statement even if more than one row in your table are updated/deleted/inserted. Be mindful of that and structure your code accordingly.

If you run into troubles with writing the code, post more details - take a look at Brett's blog here for some guidance. http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-02 : 12:23:04
One more small thing to note is UPDATE() function doesnt really look whether value got updated but will just check if column was part of UPDATE operation so checking alone with UPDATE() function may not be sufficient to know whether actual updation of value has happened

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

Go to Top of Page
   

- Advertisement -