Solved

Getting data for edited values in update trigger in sql server

Posted on 2014-03-21
4
4,265 Views
Last Modified: 2014-04-03
Hi ,
 
I am writing update trigger for tables in SQL server 2008 r2

i need to store values (Old value, new value ) in Log table.

How to get the old values  ?

CREATE TRIGGER trgAfterUpdate ON [dbo].[Employee_Test]
FOR UPDATE
AS
      declare @empid int;
      declare @empname varchar(100);
      declare @empsal decimal(10,2);
      declare @audit_action varchar(100);

      select @empid=i.Emp_ID from inserted i;      
      select @empname=i.Emp_Name from inserted i;      
      select @empsal=i.Emp_Sal from inserted i;      
      
      if update(Emp_Name)insert into insert into Emp_Audit
           (User_ID,System_Time,Affected_Table,Affected_Field,Old_Data,New_Data)
      values(@user_id,@InsertDate,'Emp','EMP_ID','',@empid);
      if update(Emp_Sal)
            insert into insert into Emp_Audit
           (User_ID,System_Time,Affected_Table,Affected_Field,Old_Data,New_Data)
      values(@user_id,@InsertDate,'Emp','EMP_sal','',@empsal);

      insert into insert into Emp_Audit
           (User_ID,System_Time,Affected_Table,Affected_Field,Old_Data,New_Data)
      values(@user_id,@InsertDate,'Emp','EMP_ID','',@empid);

      PRINT 'AFTER UPDATE Trigger fired.'
GO
0
Comment
Question by:Vikash p
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 167 total points
ID: 39945852
The old values will be stored in the deleted table.

http://technet.microsoft.com/en-us/library/ms191300.aspx
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 167 total points
ID: 39946681
The TRIGGER as designed is fatally flawed.  A TRIGGER fires per statement not per row.  Therefore in your case if your UPDATE statement affects more than one row it will fail.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 166 total points
ID: 39951034
The trigger won't "fail", that is, it won't abend; it will just capture only one row's values instead of all of the changed rows.  [In SQL Server, there is no "FOR EACH ROW" trigger, not even as an option.]
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39951983
The trigger won't "fail", that is, it won't abend; it will just capture only one row's values instead of all of the changed rows
Absolutely.  I should have clarified.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question