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

SqlServer_合并多个递归查询数据(CTE)

时间:2015-06-15 10:57:03      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

--cteChildren 子孙信息
--cteParents  父亲信息
with cteChildren(EmpID,ParentID) 
AS 
(
select EmpID,ParentID from Persons where parentid = 10171 
union all 
select t.EmpID,t.parentid from Persons as t 
inner join cteChildren as c on t.ParentID = c.Id
),
cteParents(EmpID,ParentID) 
AS 
( 
select EmpID,ParentID from Persons where Id= 10172 
union all 
select t.EmpID,t.parentid from Persons as t 
inner join cteParents as c on t.Id= c.ParentID 
) 
--合并查询
select Idfrom cteChildren 
UNION 
select Idfrom cteParents

SqlServer_合并多个递归查询数据(CTE)

标签:

原文地址:http://www.cnblogs.com/gossip/p/4576306.html

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