SQL SERVER 递归查询实现BOM多阶报表

1.向下递归

复制代码
--向下递归
with temp (OrgId,ParentId,OrgName)
as
(
select OrgId,ParentId,OrgName from Org
where OrgId='01'
union all
select a.OrgId, a.ParentId,a.OrgName from Org a
inner join temp on a.[ParentId] = temp.[OrgId]
)
select * from temp
复制代码

输出结果:

 

2.向上递归

复制代码
--向上递归
with temp (OrgId,ParentId,OrgName)
as
(
select OrgId,ParentId,OrgName from Org
where OrgId='0405'
union all
select a.OrgId, a.ParentId,a.OrgName from Org a
inner join temp on a.[OrgId] = temp.[ParentId]
)
select * from temp
复制代码

输出结果:

转载:https://www.cnblogs.com/yuyuefly/p/9684593.html  

posted @ 2025-08-05 19:06  木头侠  阅读(57)  评论(0)    收藏  举报