join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



Speed up SQL script deployment

Recursive queries with SQL Server 2000

Written By: Greg Robidoux -- 8/30/2006 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
In SQL Server 2000 there is no simple way to create recursive queries that have several levels of data (hierarchical data).  Generally a recursive query is needed when you have a parent and child data stored in the same table.  One example may be employee data where all employee data is stored in one Employee table and there is an indicator that specifies the employees supervisor that points back to another record in the same table.  This is done quite often for other data as well. 

The following shows a sample table and data.

Employee

EmpID FirstName LastName SupervisorID
1 Bob Jones NULL
2 Gina Smith 1
3 Tim Owens 1
4 Mary Jacobs 3

To query this data to find the employee and supervisor is pretty easy.  The query would be:

SELECT e1.EmpId, e1.FirstName, e1.LastName, e1.SupervisorID, e2.FirstName AS SupFirstName, e2.LastName AS SupLastName
FROM Employee e1 LEFT JOIN Employee e2 ON e1.SupervisorID = e2.EmpId

When we run this query these are the results we get:

EmpID FirstName LastName SupervisorID SupFirstName SupLastName
1 Bob Jones NULL NULL NULL
2 Gina Smith 1 Bob Jones
3 Tim Owens 1 Bob Jones
4 Mary Jacobs 4 Tim Owens

So you can see this is pretty simple to just get this level of data, but what if you want to find out all of the direct and indirect reports of Bob Jones.  Well this now becomes a bit more difficult. In this example there are only 3 levels of data, but what if you have a much larger hierarchy?

Solution
As mentioned above there is no simple way to produce recursive queries that have several levels of data, but several implementations have been produced and are available on the internet.  Finding all of these options, understanding them, implementing and testing takes some time, so to make your life a little easier we have gathered a list of some useful variations on how to produce recursive queries in SQL Server 2000.

The following solution is pretty straight forward and offers a lot of flexibility.  Although it met my needs, take a look at the other options as well to see which one makes the most sense for your needs.

This is a list of various implementations and methods for creating recursive queries for SQL Server 2000. There are probably others out there as well.

With all of these options the parentID is found and then there is a link to the child data and then that child's data etc...  One of the things that these articles also specify is creating a lineage of what records belong to which parent, grand-parent, etc...  This lineage then gets stored with the data so that when you are searching for records you can use the LIKE clause in your WHERE statement to find any records that have this value. 

Overall there is not a simple clean approach for dealing with this data, but using one or a combination of these techniques should allow you to reach your goal.

Note: With SQL Server 2005 you have the option of creating recursive queries using Common Table Expressions or CTEs. With CTEs, Microsoft has made this process a little easier then in previous versions of SQL Server.  Take a look at this post to learn more about CTEs and look for a future tip on how to use CTEs with SQL Server 2005.

Next Steps

  • Take a look at these various methods for creating recursive queries
  • Select a method that works best for your environment
  • Modify these methods to implement the right solution for your needs
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Wasting time running multiple scripts against multiple SQL Servers manually?

Try SQL Multi Script and execute all those scripts with just one mouse click.

Execute multiple scripts against multiple SQL Servers with a single click

"Just tried SQL Multi Script and very impressed with it. Talk about reducing work load!"

Neil Abrahams SQL Server DBA/Developer

SQL Multi Script Red Gate Software - ingeniously simple tools

Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.

Red Gate Software - SQL Compare

Quickly and accurately deploy database changes with Red Gate’s SQL Compare. 2/3 of people who use a SQL comparison tool use Red Gate’s SQL Compare – the industry standard database comparison and deployment tool. “We rely on SQL Compare for every deployment.” Paul Tebbut, Technical Lead, Universal Music Group

Download now!



More SQL Server Tools
SQL diagnostic manager

SQL comparison toolset

SQL Refactor

SQL compliance manager

SQL Prompt


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Free trial: Red Gate SQL Response for no-nonsense monitoring & alerting of SQL Server health & activity. Download now.

You don't know, what you don't know about SQL Server... Customized Consulting and Training

Here is your chance to win a free all-expenses-paid trip to the 2010 SQL PASS SUMMIT.

Free white paper - Top SQL Server Backup Mistakes and How to Avoid Them



Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com