Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Recursive CTE Example

By Neil Bryan,

The idea of this script is to demonstrate how a recurive common table expression can be used to derive hirearchial data. The example given is an employee table where each record pertains to an employee, which has a link to the manager record in the same table. 
Using a technique such as this is far more efficient than using cursors.
The script sets up a table variable of hirearchial employee data.
To test, set the @BossID to a valid employee ID.

Total article views: 2357 | Views in the last 30 days: 32
 
Related Articles
FORUM

sql query need Help about Absent Employee

Query about absent employee

ARTICLE

Employee Slurping

This editorial was originally published on Oct 21, 2006. It is being re-run as Steve is out at SQL i...

ARTICLE

Employee Retention

A SQL Server DBA seems to be a stable job and many of us stick with the same job for an above averag...

BLOG

Employee or 1099?

My friend Wes Dumey does a lot of work in the business intelligence sector as a contract employee an...

FORUM

Employee job mapping

Soln required for employee and job mapping query

Tags
hireachial    
recursive cte    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones