Sql server2005 递归查询

with DEPT1 as(  

        select DEPARTID ,PARENTID from DEPARTMENT  

    ),  

    DEPT2 as(  

        select * from DEPT1 where DEPARTID=8  

        union all  

        select DEPT1.* from DEPT2 inner join DEPT1   

        on DEPT2.PARENTID=DEPT1.DEPARTID  

    ) 

select * from DEPT2 

猜你喜欢

转载自hellodajun.iteye.com/blog/1929113