java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

递归删除父节点及所有子节点

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56709
--递归删除父节点及所有子节点
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
                                                                  

posted on 2009-06-12 18:24 rrong_m 阅读(1655) 评论(0)  编辑  收藏

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜