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.

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.

Recursive CTEs

From Wiki

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
  5. union all
  7. select child_query
  8. inner join parent_query
  10. }
  11. select *
  12. from cte_a

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

683 Rating: 2.5/5 (54 votes cast)