码迷,mamicode.com
首页 > 数据库 > 详细

SqlServer里,一条sql进行递归删除

时间:2014-07-13 23:18:57      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   io   div   

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 

非存储过程方法:

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) 

 

SqlServer里,一条sql进行递归删除,布布扣,bubuko.com

SqlServer里,一条sql进行递归删除

标签:style   blog   color   使用   io   div   

原文地址:http://www.cnblogs.com/wolfocme110/p/3840460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!