Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Recursive CTEs

From Wiki

Revision as of 03:21, 8 April 2010 by Naomi (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Recursive CTEs are a great way to join a table or query to its self to get parent-child data. An example instance to use a recursive CTE is if you have an Employees table. The table would have a primary key identity and a manager field that is a foreign key back to the table.

The basic structure of a Recursive CTE is:

  1. ;with cte_a as
  2. {
  3. select parent_query
  4.  
  5. union all
  6.  
  7. select child_query
  8. inner join parent_query
  9.  
  10. }
  11. select *
  12. from cte_a

See also CTE and hierarchical queries discussing this topic in more details

683 Rating: 2.6/5 (51 votes cast)