My Table contains Three columns and the value looks like the following
Emp_ID | Emp_Name | Emp_Manager_ID
========================================
1 | Admin | Null
2 | John | 1
3 | Sam | 2
4 | Mike | 2
5 | Jeff | 4
6 | Ben | 3
7 | Vicky | 5
The parameter id @Emp_ID = 2
The expected result to find all the subordinates under the given Emp_Id
so the result should be all EmpIDs 3,4,5,6,7
because 2 is the manager of 3,4 and 3 is the manager of 6, 4 is the manager of 5 and 5 is the manager of 7
Using a Recursing CTE. This currently returns all three columns. Remove
Emp_NameandEmp_Manager_IDfrom theSELECTif you do not require that information.Example of query running using Employee_ID = 1: