Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 191955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:23:07+00:00 2026-05-11T16:23:07+00:00

Consider the following simple DAG: 1->2->3->4 And a table, #bar, describing this (I’m using

  • 0

Consider the following simple DAG:

  1->2->3->4

And a table, #bar, describing this (I’m using SQL Server 2005):

parent_id   child_id
1           2
2           3
3           4
//... other edges, not connected to the subgraph above

Now imagine that I have some other arbitrary criteria that select the first and last edges, i.e. 1->2 and 3->4. I want to use these to find the rest of my graph.

I can write a recursive CTE as follows (I’m using terminology from MSDN):

with foo(parent_id,child_id) as (
// anchor member that happens to select first and last edges:
select parent_id,child_id from #bar where parent_id in (1,3)
union all
// recursive member:
select #bar.* from #bar
join foo on #bar.parent_id = foo.child_id
)
select parent_id,child_id from foo

However, this results in edge 3->4 being selected twice:

parent_id  child_id
1          2
3          4
2          3
3          4    // 2nd appearance!

How can I prevent the query from recursing into subgraphs that have already been described? I could achieve this if, in my “recursive member” part of the query, I could reference all data that has been retrieved by the recursive CTE so far (and supply a predicate indicating in the recursive member excluding nodes already visited). However, I think I can access data that was returned by the last iteration of the recursive member only.

This will not scale well when there is a lot of such repetition. Is there a way of preventing this unnecessary additional recursion?

Note that I could use “select distinct” in the last line of my statement to achieve the desired results, but this seems to be applied after all the (repeated) recursion is done, so I don’t think this is an ideal solution.

Edit – hainstech suggests stopping recursion by adding a predicate to exclude recursing down paths that were explicitly in the starting set, i.e. recurse only where foo.child_id not in (1,3). That works for the case above only because it simple – all the repeated sections begin within the anchor set of nodes. It doesn’t solve the general case where they may not be. e.g., consider adding edges 1->4 and 4->5 to the above set. Edge 4->5 will be captured twice, even with the suggested predicate. 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:23:08+00:00Added an answer on May 11, 2026 at 4:23 pm

    The CTE‘s are recursive.

    When your CTE‘s have multiple initial conditions, that means they also have different recursion stacks, and there is no way to use information from one stack in another stack.

    In your example, the recursion stacks will go as follows:

    (1) - first IN condition
    (1, 2)
    (1, 2, 3)
    (1, 2, 3, 4)
    (1, 2, 3) - no more children
    (1, 2) - no more children
    (1) - no more children, going to second IN condition
    
    (3) - second condition
    (3, 4)
    (3) - no more children, returning
    

    As you can see, these recursion stack do not intersect.

    You could probably record the visited values in a temporary table, JOIN each value with the temptable and do not follow this value it if it’s found, but SQL Server does not support these things.

    So you just use SELECT DISTINCT.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok this looks like a major fundamental bug in .NET: Consider the following simple
Consider the following simple C program that read a file into a buffer and
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following SQL: BEGIN TRAN SET TRANSACTION ISOLATION LEVEL READ COMMITTED INSERT Bands
Consider the following simple html page markup: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
Consider the following simple RESTEasy (JAX-RS) service: @Path(/example-service) public interface ExampleService { @Path(/ping) @GET
Consider the following simple code pattern: foreach(Item item in itemList) { if(item.Foo) { DoStuff(item);
Consider the following simple example: One team has many players and a player may
Please consider the following simple use case: public class Foo { public virtual int
I know this is a simple question, but I can't figure it out. Consider

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.