[SQL SERVER]CTE應用-階層資料表
網友問題,紀錄一下利用CTE輸出該資料表結構
;with mycte
as
(
select *,0 as [level],id as [groupid]
from ##mytest where pid='root'
union all
select t1.*,t2.level+1,t2.groupid
from ##mytest t1 join mycte t2 on t1.pid=t2.id
)
select t1.* from mycte t1
order by t1.groupid, t1.level