Solved

Getting data for edited values in update trigger in sql server

Posted on 2014-03-21
4
4,227 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

738 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