cte递归

--不解释。。
WITH    SimpleRecursive ( Employee_NM, Employee_ID, ReportsTo ,Sublevel)
          
AS ( SELECT   Employee_NM ,
                        Employee_ID ,
                        ReportsTo,
                        
0
               
FROM     Employee_Tree
               
WHERE    Employee_ID = 2
               
UNION ALL
               
SELECT   p.Employee_NM ,
                        p.Employee_ID ,
                        p.ReportsTo,
                        Sublevel 
+ 1
               
FROM     Employee_Tree p
                        
INNER JOIN SimpleRecursive A ON A.Employee_ID = p.ReportsTo
             )
    
SELECT  sr.Employee_ID AS empid ,
            sr.Employee_NM 
AS Emp ,
            et.Employee_NM 
AS Boss      
    
FROM    SimpleRecursive sr
            
INNER JOIN Employee_Tree et ON sr.ReportsTo = et.Employee_ID
            
WHERE    Sublevel <=2

 

posted @ 2010-10-03 05:07  小白.net  阅读(206)  评论(0编辑  收藏  举报