时间:2021-07-01 10:21:17 帮助过:2人阅读
1,创建触发器
<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 comments will not be included in -- the definition of the function. -- ================================================ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= CREATE TRIGGER trigCategoryDelete ON category instead of DELETE AS BEGIN declare @caID int select @caID=id from deleted --删除评论 delete comment where newsId in (select newsId from news where caID =@caID ) --删除新闻 delete news where caID=@caID --删除新闻类别 delete category where id=@caID END GO </span>
2,修改触发器
<span style="font-size:18px;">USE [newssystem] GO /****** Object: Trigger [dbo].[trigCategoryDelete] Script Date: 06/10/2015 16:48:45 ******/ SET ANSI_NULLS ON GO SETQUOTED_IDENTIFIER ON GO --============================================= --Author: tsj -- Create date: 2015年6月9日16:40:00 --Description: 删除类别触发器 --============================================= ALTER TRIGGER[dbo].[trigCategoryDelete] ON [dbo].[category] AFTER delete AS BEGIN --select* from deleted declare @caId int ---定义变量 select @caId=id from deleted -----从 deleted中取出id --删除评论 deletecomment where newsId=(select newsId from news where caID=@caId) --删除新闻 deletenews where caID=@caID --删除类别 deletecategory where id=@caID END </span>
3,删除触发器:
<span style="font-size:18px;">drop trigger trigger_name </span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
SQL Server 触发器
标签: