sql中递归算法

;with t AS

(

SELECT id FROM dbo.tkf_OpDepartment where id =1

union all

select b.id

from dbo.tkf_OpDepartment as b,t where b.ParentID=t.id  

)

select ltrim(id) as col from t order by id

—————————————————-本级及子集————————————

;with t AS
(
SELECT *,[level]=cast(” as varchar(50)) FROM tkf_OpDepartment where ParentID =0
union all
select b.*,[level]=cast(‘,’+ltrim(b.ParentID)+[level] as varchar(50))
from tkf_OpDepartment as b,t where b.ParentID=t.id
)
select ltrim(id)+[level] as col from t order by id

    原文作者:递归算法
    原文地址: https://blog.csdn.net/caiyang101/article/details/53894366
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞