Solved

Getting data for edited values in update trigger in sql server

Posted on 2014-03-21
4
4,139 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
  • 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:ScottPletcher
ScottPletcher 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now