当前位置: 代码迷 >> Sql Server >> 来看看 关于触发器触发删除下级时出现的有关问题
  详细解决方案

来看看 关于触发器触发删除下级时出现的有关问题

热度:99   发布时间:2016-04-27 17:20:32.0
高手进来看看 关于触发器触发删除下级时出现的问题
我的一个表为[pro_categories]
为了实现删除节点时触发删除下级,创建触发器为:
CREATE TRIGGER [dbo].[DeletePcChild] ON [dbo].[pro_categories]
FOR DELETE
AS
BEGIN  
DELETE FROM pro_categories WHERE pcParentID IN(
SELECT pcID from deleted )
END
比如说有节点AAA,AAA下面有子节点BBB,BBB下面有子节点CCC,为什么删除AAA时只能删除BBB,不能同时删除CCC??
也就是说只能触发下级的删除,删除BBB时为什么不能连带触发删除CCC呢?
求高手解答!!

------解决方案--------------------
SQL code
CREATE TRIGGER [dbo].[DeletePcChild] ON [dbo].[pro_categories]FOR DELETEASBEGIN   ;with cte as(select pcid from pro_categories where pcParentid in(select pcID from deleted)union allselect a.pcid from pro_categories a inner join cte b on a.pcParentid=b.pcid)delete from pro_categories where pcid in(select pcid from cte) end
  相关解决方案