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

sql 循环

时间:2019-03-02 10:55:52      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:nio   position   arc   pos   结构   team   parentId   var   pre   

--表结构  SELECT id,position,Parentid FROM op_client_sales_structure 

WITH TEST_CTE AS ( SELECT id,position,Parentid,Cast(Parentid AS NVARCHAR(
4000)) AS PATH FROM op_client_sales_structure team WHERE Parentid !=-1 UNION ALL SELECT a.id,a.position,a.Parentid, CTE.PATH+,+Cast(a.Parentid AS NVARCHAR(4000)) AS PATH FROM op_client_sales_structure a INNER JOIN TEST_CTE CTE ON a.id=CTE.Parentid ) SELECT * FROM TEST_CTE WHERE Parentid=(SELECT id FROM op_client_sales_structure WHERE Parentid=-1) --限制递归次数 OPTION(MAXRECURSION 10)

 

sql 循环

标签:nio   position   arc   pos   结构   team   parentId   var   pre   

原文地址:https://www.cnblogs.com/shuaimeng/p/10459891.html

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