SQL Server triggers

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.