with cte_child(id,areaName,pid)
as
(
–起始条件
select id,areaName,pid from erp_area
where id = 44 –列出子节点查询条件
union all
–递归条件
select a.id,a.areaName,a.pid
from erp_area a
inner join
cte_child b –执行递归
on a.id=b.pid
)
select * from cte_child;
//如何递归查询层级数据将父级字段和本级某个字段合并
select stuff((
select ‘–‘+subjectname
from cte_child order by subjectid desc for xml path(”)),1,0,”) as subjectname ;
原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/tech/database/275494.html