MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - More About Common Table Expressions in SQL...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

More About Common Table Expressions in SQL Server 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 12
    2006-06-12

    Table of Contents:
  • More About Common Table Expressions in SQL Server 2005
  • A recursive common table expression: explanation
  • Displaying the depth of recursion when using recursive CTE: listing levels
  • Displaying the depth of recursion when using recursive CTE: explanation
  • A recursive common table expression using views: example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    More About Common Table Expressions in SQL Server 2005 - A recursive common table expression: explanation


    (Page 2 of 5 )

    This section explains the script provided in the previous section.  Let me explain it part by part.  Let us first consider the following code fragment:

    SELECT
      EmployeeID, ManagerID
     FROM HumanResources.Employee
     WHERE ManagerID = 140

    It is not surprising that it retrieves the list of all employees working under manager 140.  Let us add a very funny structure to the above SELECT statement which ends up as follows:

    WITH EmpCTE
    AS
    (
     SELECT
      EmployeeID, ManagerID
     FROM HumanResources.Employee
     WHERE ManagerID = 140

    Even though we don’t come across any remarkable modifications, it has its own depth. Every row retrieved by the SELECT statement will be treated as part of the temporary table named “EmpCTE.”  At this point, it is not of much interest.  Let us proceed with the following statement now:

    SELECT
      b.EmployeeID, b.ManagerID
     FROM HumanResources.Employee b
     JOIN EmpCTE AS a ON a.EmployeeID = b.ManagerID

    This is the most confusing statement you would ever see (or understand).  The above statement works with every row present in “EmpCTE.”  It primarily retrieves all the subordinates of each employee existing in “EmpCTE” and adds them again to the same “EmpCTE” with the help of the UNION ALL operator. 

    This process gets executed recursively for every new row added (or being added) to the same “EmpCTE.”  Finally we retrieve all the information from “EmpCTE” using the following statement:

    SELECT EmployeeID,ManagerID
    FROM EmpCTE

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys, enjoy another contribution here on CTE with SQL Server 2005.
     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT