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

SqlServer with递归查询的使用

时间:2018-05-11 20:22:14      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:union   http   serve   info   inner   sip   www   等于   www.   

SqlServer with递归查询的使用

 

with tempAgent (AgentId,ParentId)

as(

select AgentId,ParentId from AgentInfo where AgentId=2 --要查询的根节点

union all

select a.AgentId,a.ParentId from AgentInfo a   --所有查询出所有的数据

inner join tempAgent on a.ParentId=tempAgent.AgentId --查询父节点等于Id的数据

)

 

select * from tempAgent

 

 

AgentId ParentId

2 1

7 2

8 2

9 7

SqlServer with递归查询的使用

标签:union   http   serve   info   inner   sip   www   等于   www.   

原文地址:https://www.cnblogs.com/eimers/p/9025619.html

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