Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
Im using triggers,for example im having a table EmployeeDetails with EmpId and EmpName. I have already inserted 2 records into it called "01,Kamal and 02,Surya". By using AfterUpdate trigger,im updating the 2nd employee name as "Raj". Now how to retrieve the both the data of EmployeeDetails before and after updation of a table ie i want details as before updation name should be as "Surya" and after updation name should be as "Raj" in a same table...guide me soon
Posted
Comments
Santhosh Kumar Jayaraman 17-Aug-12 8:04am    
Before update select * and after update do one more select * for that particular row.

Create Both tables.

SQL
 Create table Emp (
EmpId  varchar(5),
EmpName varchar(40)
)
Create table EmployeeDetailsHist (
EmpId  varchar(20)
, EmpName varchar(40)
, HistoryDate datetime 
) 

After update Trigger
SQL
create TRIGGER EmpAfterUpdate
ON Emp
AFTER UPDATE
AS
IF ( UPDATE (EmpId) OR UPDATE (EmpName) )
BEGIN
INSERT INTO EmployeeDetailsHist (EmpId, EmpName, HistoryDate)
select EmpID, EmpName, getdate()
from deleted ;
END;


Existing data

1 Kamal
2 surya
Update Emp set EmpName = 'Raj' where EmpId = '1'

select 
'Current' as Origin, EmpId, EmpName from Emp 
union all
select 'History' as Origin, EmpId, EmpName from EmployeeDetailsHist


Result is:
Status    EmpId   Name 
------------------------
Current	1	Raj
Current	2	Surya
History	1	Kamal
 
Share this answer
 
v4
No options like that, You have to craft 2 select statement to show the updated data and unupdated data. Mr Sachin have give easy and good eg.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900