递归删除父节点及所有子节点(转)
生活随笔
收集整理的這篇文章主要介紹了
递归删除父节点及所有子节点(转)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
--遞歸刪除父節(jié)點及所有子節(jié)點
create table tb(Id int, ParentId int, Name varchar(5))
insert into tb select 1, 0, 'a1'
union all select 2,2, 'a2'
union all select 14, 1, 'b11'
union all select 15, 1, 'b12'
union all select 16, 14, 'c13'
union all select 17, 14, 'c14'
union all select 104,17,'d15'
go
WITH temptab(id, parentid, name) AS
( SELECT root.id, root.parentid, root.name
FROM tb root
WHERE id=1
UNION ALL
SELECT sub.id, sub.parentid, sub.name
FROM tb sub, temptab super
WHERE sub.parentid = super.id
)
delete from tb where id in(
select id from temptab
)
select * from tb
go
drop table tb
/*
Id ParentId Name
----------- ----------- -----
2 2 a2
create table tb(Id int, ParentId int, Name varchar(5))
insert into tb select 1, 0, 'a1'
union all select 2,2, 'a2'
union all select 14, 1, 'b11'
union all select 15, 1, 'b12'
union all select 16, 14, 'c13'
union all select 17, 14, 'c14'
union all select 104,17,'d15'
go
WITH temptab(id, parentid, name) AS
( SELECT root.id, root.parentid, root.name
FROM tb root
WHERE id=1
UNION ALL
SELECT sub.id, sub.parentid, sub.name
FROM tb sub, temptab super
WHERE sub.parentid = super.id
)
delete from tb where id in(
select id from temptab
)
select * from tb
go
drop table tb
/*
Id ParentId Name
----------- ----------- -----
2 2 a2
轉載于:https://www.cnblogs.com/jiajiayuan/archive/2011/09/02/2163779.html
總結
以上是生活随笔為你收集整理的递归删除父节点及所有子节点(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信息化基础建设系列文章汇总 .NET 平
- 下一篇: 产品设计眼中的运营,互联网营销