when I was studying beef brisket, I first formally implemented it in SQL Server Trigger, he is similar to a stored procedure , which can be said that a trigger is a special type of stored procedure, but the trigger is triggered by an event is automatically invoked, and stored procedures can be called through the name of the stored procedure.
1, creating a trigger
<span style= "FONT-SIZE:18PX;" >--================================================--template generated from template Explorer using:--Create Trigger (New Menu). SQL----Use the Specify values for Template Parameters--command (CTRL-SHIFT-M) to fill in the parameter-values below. ----See additional Create Trigger templates for more--Examples of different Trigger statements.----This block of commen TS won't be included in--the definition of the function.--================================================set Ansi_nu LLS ongoset quoted_identifier ongo--=============================================--Author:<Author,,Name>-- Create Date: <create date,,>--description:<description,,>--========================================== ===create TRIGGER trigcategorydelete on category instead of Deleteas begindeclare @caID intselect @caID =id from Delet ed--Delete comment Delete comment where newsId in (select NewsId from news where CaID [email protected])--Delete news Delete news where [email protected]--Delete News categories delete category where [email protected] endgo</span>
2, modify trigger
<span style= "FONT-SIZE:18PX;" >use [newssystem]go/****** Object: Trigger [dbo].[ Trigcategorydelete] Script date:06/10/2015 16:48:45 ******/set ansi_nulls ongosetquoted_identifier ONGO--======= ======================================--author: tsj--Create date:2015 June 9 16:40:00--description: Delete Category trigger--=============================================alter trigger[dbo]. [Trigcategorydelete] on [dbo]. [Category] After Deleteas begin--select* from deleteddeclare @caId int---definition variable select @caId =id from deleted-----Remove deleted from id--Delete Comment D eletecomment where newsid= (select NewsId from news where [email protected])--Delete news deletenews where [email protected]--delete class Don't deletecategory where [email protected] end</span>
3, delete the trigger:
<span style= "FONT-SIZE:18PX;" >drop Trigger Trigger_name </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SQL Server triggers