首页 > 代码库 > SqlServer里,一条sql进行递归删除
SqlServer里,一条sql进行递归删除
Server 2005中提供了公用表表达式(CTE),使用CTE,可以使SQL语句的可维护性,同时,CTE要比表变量的效率高得多。
存储过程方法:
1 create proc up_delete_nclass
2 @did int
3 as
4 with my1 as(select * from News_Class where id = @did
5 union all select News_Class.* from my1, News_Class where my1.id = News_Class.ParentID
6 )
7 delete from News_Class where exists (select id from my1 where my1.id = News_Class.id)
8 go
9
10 exec up_delete_nclass 16
2 @did int
3 as
4 with my1 as(select * from News_Class where id = @did
5 union all select News_Class.* from my1, News_Class where my1.id = News_Class.ParentID
6 )
7 delete from News_Class where exists (select id from my1 where my1.id = News_Class.id)
8 go
9
10 exec up_delete_nclass 16
非存储过程方法:
1 with my1 as(select * from aaa where id = 1
2 union all select aaa.* from my1, aaa where my1.id = aaa.pid
3 )
4 delete from aaa where exists (select id from my1 where my1.id = aaa.id)
2 union all select aaa.* from my1, aaa where my1.id = aaa.pid
3 )
4 delete from aaa where exists (select id from my1 where my1.id = aaa.id)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。