组织机构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


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

posted @ 2015-10-30 14:11  行野摄色  阅读(465)  评论(0编辑  收藏  举报