|
|
|
Here is very basic code to write trigger for INSERT, UPDATE or DELETE operation on a table. Internally SQL Server manages two tables (inserted and deleted) to perform trigger operations.
All inserted records can be retrived by quering "inserted" table |
All deleted records can be retrived by quering "deleted" table |
All updated records can be retrived by joining with inserted and deleted tables. Since there is no seperate table named "updated" for updated records so you have to find updated records by joining "inserted" and "deleted" tables |
Click here to copy the following block | set nocount on go if object_id('tr_tbl') is not null drop table tr_tbl go create table tr_tbl(i int) go create trigger tr_test on tr_tbl for insert, update, delete as if exists(select * from inserted) and exists(select * from deleted) print 'Update...' else if exists(select * from inserted) print 'Insert...' else print 'Delete...' go
insert tr_tbl values(1) update tr_tbl set i = i + 1 delete tr_tbl |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|