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

该方法在数据量过大时,效率过低,可参考hierarchyid字段实现(Sqlserver 2008)
优点:效率较高
缺点:需要不断维护数据,对现有业务有一定影响
参考:http://www.cnblogs.com/luminji/p/4403569.html
 
--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 Id from cteChildren 
UNION 
select Id from cteParents
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值