STDLN February 2016

How to create a trigger that copies into another table the original and edited data from a table?

I am using SQL Server and i am wondering if there is a way to create a trigger on a table which allows to copy its original data before an update and also copy its modified data after the update in another table?

If yes, how could i make the difference between both?

Thanks :)


@SouravA

create trigger SomeBeforeUpdateTrigger
on YourTable
before update
as
insert into SecondTable (ID, Name)
select ID, Name
from deleted; 
go
insert into SecondTable (ID, NewName)
select ID, Name
from inserted;

Answers


SouravA February 2016

If I understand correctly, yes it is possible. You would basically need a BEFORE UPDATE trigger.

create trigger SomeBeforeUpdateTrigger
on YourTable
before update
as
insert into BeforeUpdate (ID, Name)
select ID, Name
from deleted; 
go
insert into AfterUpdate (ID, Name)
select ID, Name
from inserted;    

The first statement will insert those rows into the table BeforeUpdate which are getting changed. The second will insert the new rows into the table AfterUpdate.

Post Status

Asked in February 2016
Viewed 1,960 times
Voted 8
Answered 1 times

Search




Leave an answer