复习下sql server 的触发器

创建触发器前要写上是否存在某个触发器,如果存在,则删掉它

--创建insert插入类型触发器
if (object_id('tgr_classes_insert', 'tr') is not null) //红色部分自己不太熟悉,括号也要记得加上
drop trigger tgr_classes_insert

执行或修改触发器的语法是alert(大小写都可以哦!)

--以下是自己创建的,该触发器可以删除主键表和外键表相关联的数据
if (object_id('deleteTopic', 'tr') is not null)  //红色部分自己不太熟悉,括号也要记得加上
drop trigger tgr_classes_insert

CREATE TRIGGER [deleteTopic]
ON [dbo].[bbsTopic]
AFTER delete
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @topicId int
delete bbsTopic where topicId=@topicId
delete bbsTopicCollect where topicId=@topicId

END

posted @ 2014-10-06 11:20  巴拉比巴拉  阅读(102)  评论(0编辑  收藏  举报