?
Solved

Getting data for edited values in update trigger in sql server

Posted on 2014-03-21
4
Medium Priority
?
4,328 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 668 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 668 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 664 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

752 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