最近整理字典,頻繁用到遞歸查詢,老是記不住,留個代碼以後備用!
with cte as
(select Zdxmc ,(select Zdxmc from GC_Sys_Zdx where Zdxid = a.Parentid) as 'Parent',a.Parentid from GC_Sys_Zdx a where Bz = '現從事專業'
union all
select b.Zdxmc,(select Zdxmc from GC_Sys_Zdx where Zdxid = b.Parentid) as 'Parent',b.Parentid from cte,GC_Sys_Zdx b where cte.Parentid = b.Zdxid
)
select * from cte