ADVERTISEMENT
More Stuff
Update Trigger is very useful if you keep your deleted record or which data is replaced withnew,, here I have one table called tblstudent which have a user details, user may update their information time to time and i have to keep their deleted data securely, so lets discuss how it is possible lets create table tblstudent

create table tblstudent
(
studentid int primary key,
studentname varchar(max),
studentaddress varchar(max),
grade varchar(10),
isactive bit
)
Now I have to create one more table call tblstudent_log which store the updated data from tblstudent
create table tblstudent_log
(
studentid int,
studentname varchar(max),
studentaddress varchar(max),
grade varchar(10),
isactive bit,
updatedate
)
Now I am going to create the trigger
Create trigger [dbo].[updateforstudent]
on tblstudent
for Update
as
begin
insert into tblstudent_log
(
select studentid,
studentname,
studentaddress,
grade,
isactive,
getdate()
from deleted
)
end
Here updateforstudent is a trigger name to execute whenever the update to tblstudent, because we are creating this trigger on tblstudentand for the update this statement tell to execute whenever update command pass to tblstudent
studentname,
studentaddress,
grade,
isactive,
getdate()
from deleted,
deleted is a magic table which store the deleted data when we update the tblstudent
0 Comments