作用:删除新闻类别时,通过触发器,自动删除掉类别下的新闻,以及新闻所对应的评论。一次完成。
如下代码写在操作表的触发器下
如下代码写在操作表的触发器下
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: 王翔
-- Create date:
-- Description: 删除类别触发器
-- =============================================
ALTER TRIGGER [trigCategoryDelete]
ON [dbo].[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
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: 王翔
-- Create date:
-- Description: 删除类别触发器
-- =============================================
ALTER TRIGGER [trigCategoryDelete]
ON [dbo].[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