--Creating Trigger--
Create trigger [dbo].[IF_Update_Trigger_Trig]
on [dbo].[IF_Update_Trigger]
after update as
if(COLUMNS_UPDATED() & 10)>0
begin
----inserting old values
insert into IF_Update_Trigger_Audi
(
ID,Changed_Status,Name,Emp_code,Salary,Sys_User,Modified_Date
)
select
d.ID,
'OLD',
d.Name,
d.Emp_code,
d.salary,
SYSTEM_USER,
CURRENT_TIMESTAMP
from deleted d;
--inserting updated values
insert into IF_Update_Trigger_Audi
(
ID,Changed_Status,Name,Emp_code,salary,Sys_User,Modified_Date
)
select
i.ID,
'New',
i.Name,
i.Emp_code,
i.Salary,
SYSTEM_USER,
CURRENT_TIMESTAMP
from inserted i;
end
--Main Table--
create table IF_Update_Trigger
(
ID int unique identity(1,1),
Name Varchar(30),
Emp_code int,
Salary int
)
---Inserting records into main table---
insert into IF_Update_Trigger values('Masum',123,5000)
insert into IF_Update_Trigger values('Reza',124,6000)
--Tracker Table--
create table IF_Update_Trigger_Audi
(
ID int,
Changed_Status varchar(10),
Name varchar(30),
Emp_code int,
Salary int,
Sys_User varchar(30),
Modified_Date date
)
update IF_Update_Trigger set Salary=2000 where Salary=6000
select * from IF_Update_Trigger_Audi order by Modified_Date desc
Create trigger [dbo].[IF_Update_Trigger_Trig]
on [dbo].[IF_Update_Trigger]
after update as
if(COLUMNS_UPDATED() & 10)>0
begin
----inserting old values
insert into IF_Update_Trigger_Audi
(
ID,Changed_Status,Name,Emp_code,Salary,Sys_User,Modified_Date
)
select
d.ID,
'OLD',
d.Name,
d.Emp_code,
d.salary,
SYSTEM_USER,
CURRENT_TIMESTAMP
from deleted d;
--inserting updated values
insert into IF_Update_Trigger_Audi
(
ID,Changed_Status,Name,Emp_code,salary,Sys_User,Modified_Date
)
select
i.ID,
'New',
i.Name,
i.Emp_code,
i.Salary,
SYSTEM_USER,
CURRENT_TIMESTAMP
from inserted i;
end
--Main Table--
create table IF_Update_Trigger
(
ID int unique identity(1,1),
Name Varchar(30),
Emp_code int,
Salary int
)
---Inserting records into main table---
insert into IF_Update_Trigger values('Masum',123,5000)
insert into IF_Update_Trigger values('Reza',124,6000)
--Tracker Table--
create table IF_Update_Trigger_Audi
(
ID int,
Changed_Status varchar(10),
Name varchar(30),
Emp_code int,
Salary int,
Sys_User varchar(30),
Modified_Date date
)
--Updating main table
update IF_Update_Trigger set Salary=1000 where Salary=5000update IF_Update_Trigger set Salary=2000 where Salary=6000
--Updates main table and all effected records are inserted into the tracker table.
select * from IF_Update_Triggerselect * from IF_Update_Trigger_Audi order by Modified_Date desc
No comments:
Post a Comment