27,579
社区成员
发帖
与我相关
我的任务
分享
;with t as (
select *,convert(varchar(max),内码) as pth from #t1 where 隶属关系=0
union all
select a.*,b.pth+','+convert(varchar,a.内码) from #t1 a,t b where a.隶属关系=b.内码
)
select a.内码,b.内码
from #t1 a
outer apply (
select * from t
where 内码<>a.内码
and charindex(','+convert(varchar,a.内码)+',',','+pth+',')>0
) b
order by a.内码,b.内码