SqlServer遞歸查詢

最近整理字典,頻繁用到遞歸查詢,老是記不住,留個代碼以後備用!


 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 

發佈了19 篇原創文章 · 獲贊 7 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章