Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Query help

Author  Topic 

editech
Starting Member

4 Posts

Posted - 2007-05-11 : 11:48:38
I have an employee table .Employee table has columns employeeno and manager no ,manager name etc etc.
Below is the table data

EmployeeNo | EmployeeName| ManagerNo | MangerName
001 | Ed | 005 | John
002 | joe | 001 | Ed
003 | Jim | 002 | Joe
004 | Randy | 003 | Jim
005 | John | |


Since John is manager of Ed and Ed is manager of Joe and Joe is the manager of Jim and Jim is the manager of Randy , I want to display John as a manager to all since he at the top of hierarchy.

How should I write the query to get the below results
EmployeeNo | ManagerNo | Role
001 | 005 | manager
002 | 005 | manager
003 | 005 | manager
004 | 005 | manager

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-05-11 : 12:52:55
Write a function that recursively looks for a record with ManagerNo = '' and get the EmployeeNum for that record.

Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

editech
Starting Member

4 Posts

Posted - 2007-05-11 : 16:26:27
Its just that I dont know how to write recursive SP.
I need to pass an employeeno and it should return everybody below that person.
Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2007-05-11 : 16:53:57
Here's a link that may be able to help you get started:

http://www.sql-server-helper.com/functions/get-tree-path.aspx

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page
   

- Advertisement -