Sample Table
Emp Id Emp Name Manager Id
1001 arun 1004
1002 Bharath 1004
1003 Chitra 1004
1004 Devi 1005
1005 Eli 1006
1006 Fatima 1007
1007 Ganesh 1008
when i select manager id 1004 it should display three names arun,bharath,chitra and if i select 1005 it should display devi,arun,bharath,chitra
FYI – ManagerId is same as the EMPId.
How can I prepare the sql with out using functions?
If you are using SQL Server 2005 and later you can use a common-table expression. In this example, I’m assuming that ManagerId being null indicates the top-most manager.
See Recursive Queries for more.