检索 SQL Server 2005 中的层次结构

Retrieving hierarchy in SQL Server 2005

我在数据库中有一个 table

EmployeeID                TeamLeadID
2                          1
3                          2
4                          3
5                          NULL
1                          NULL
6                          1
7                          2
8                          3

现在我想要的是检索给定 EmployeeID

的所有 TeamLeadsID(即所有上层)

而对于 5 和 1,从 table 本身

可以清楚地看到它应该是 Null

最小工作量为: