现在的位置: 首页 > 综合 > 正文

SQL递归查询

2013年01月18日 ⁄ 综合 ⁄ 共 250字 ⁄ 字号 评论关闭
在sql2005加入了cte实现sql递归,大致语法如下: 

WITH batchTable(batch) AS

 (
     select 8000 batch
     UNION ALL
     SELECT batch+1 from batchTable where batch+1<8180
 )
 select * from batchTable OPTION (MAXRECURSION 1000)

要点一:实现递归查询一定要有递归出口,否则就成了死循环了

要点二:OPTION (MAXRECURSION 1000) 规定最大的递归次数为1000

抱歉!评论已关闭.