Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Hierarchieabfrage sql server 2008

Sehen Sie sich Rekursive Abfragen an Allgemeine Tabellenausdrücke verwenden

declare @EmpID int = 3;

with C as
(
  select E.EmployeeId,
         E.Name,
         E.ManagerId
  from YourTable as E
  where E.EmployeeId = @EmpID
  union all
  select E.EmployeeId,
         E.Name,
         E.ManagerId
  from YourTable as E
    inner join C  
      on E.EmployeeId = C.ManagerId
)
select C.Name
from C

SE-Daten