Latest

6/recent/ticker-posts

Header Ads Widget

Get Job Vacancy Notice, Download Syllabus and More Job Related.

Update Trigger for SQL Server

ADVERTISEMENT
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

Update Trigger for Sql Server



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

same thing I insert to tblstudent_log by selecting data from theselect studentid,

studentname,
studentaddress,
grade,
isactive,
getdate()
from deleted,

 deleted is a magic table which store the deleted data when we update the tblstudent

ADVERTISEMENT
If You are going to Post a Comment, Please keep in mind that all comments are moderated and only comments that are relevant to the post topic will published. Let's have a meaningful conversation relevant to the post topic. THANK YOU. Check privacy policy HERE .

Post a Comment

0 Comments