日期:2014-05-18  浏览次数:20412 次

高手进来看看 关于触发器触发删除下级时出现的问题
我的一个表为[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 DELETE
AS
BEGIN   
;with cte as(
select pcid from pro_categories where pcParentid in(select pcID from deleted)
union all
select 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