6,129
社区成员
发帖
与我相关
我的任务
分享
ALTER DATABASE dbname ---这里是数据库名字
SET RECURSIVE_TRIGGERS on
create table pro_categories(pcid int,pcparentid int)
insert into pro_categories select 1,0 union all select 2,1 union all select 3,1
union all select 4,3 union all select 5,4 union all select 6,2 union all select 7,5
go
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
go
delete pro_categories where pcid=3
select * from pro_categories
/*
pcid pcparentid
----------- -----------
1 0
2 1
6 2
(3 行受影响)
*/
go
drop table pro_categories
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