Recursive queries

  • Hello,

    What's the syntax for writing recursive queries for 2 levels?

    I tried something like the following but it didn't work.

    with Recurse_1 as

    (

    -- select statement

    )

    select * From Recurse_1

    with Recurse_2 as

    (

    select * From Recurse_1

    )

    select * From Recurse_2

  • Example here:

    http://msdn2.microsoft.com/en-us/library/ms186243.aspx

    CTE has an "anchor" statement, UNION'ed with another SELECT that references the CTE.

  • Sorry, I don't quite follow. How do I save the following to another CTE for use in another recursive call:

    with Recurse_1 as

    (

    -- select statement

    )

    select * From Recurse_1

  • A CTE declaration can only be used once, so if you need to SELECT out of the CTE#1 *and* then re-use it in a subsequent CTE, maybe you'd be better putting the results if CTE#1 into a temp table ?

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply