組織機構sql

with cte as
(
    select vcOrganID, vcParentID, vcOrganName, 0 as lvl from tbOrgan
    where vcOrganID = 2
    union all
    select d.vcOrganID, d.vcParentID, d.vcOrganName, lvl+1
     from cte c inner join tbOrgan d on c.vcOrganID = d.vcParentID
)
select * from cte
it


select
    vcOrganID, vcParentID, vcOrganName,vcOrder,
    STUFF((select ',' + vcOrganID from tbOrgan
            where o.vcOrder like vcOrder+'%' and o.vcOrder<>vcOrder
            order by vcOrder
            FOR XML PATH('')), 1, 1, '') as parentIds
from tbOrgan o
order by vcOrder io

相關文章
相關標籤/搜索