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


--向下递归
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

 

 
 

 

 
--向上递归
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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM