Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
For example:

I have a table in which I maintain all employees and the employees they report to. Now, If I want to know who is report to whom based on a single employees, how can I achieve that list of all employees recursively.

Table Structure is TABLE : (EmpId, ReportTo)

Employee 1 is report to none, ReportTo = 0
Employee 2 is Reporting to Employee 1, ReportTo = 1
Employee 3 is Reporting to Employee 1, ReportTo = 1

Employee 4 is Reporting to Employee 2, ReportTo = 2
Employee 5 is Reporting to Employee 2, ReportTo = 2
Employee 6 is Reporting to Employee 4, ReportTo = 4
Employee 7 is Reporting to Employee 5, ReportTo = 5

Now If I want to know recursively the chain of all the subordinates of
Employee 1. How would that be possible by query?

Any help would be highly thankful...


Regards,

Jawwad!
Posted
Updated 24-Feb-10 3:06am
v2

See the following article on SQL Recursion;

http://msdn.microsoft.com/en-us/library/ms186243.aspx[^]

It has an Employee Example like you want.
 
Share this answer
 
yes you cane do it easyli is sql server 2005....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900