Problem with recursive query

  • I can´t find whats wrong with this recursive query:

    WITH temp (Id, ParentId, iteration)

    AS

    (

    SELECT Id, ParentId, 0 AS iteration

    FROM OrgTable WHERE ParentId IS Null

    UNION ALL

    SELECT a.Id, b.ParentId, a.iteration + 1

    FROM temp a

    JOIN OrgTable b

    on b.Id = a.ParentId

    )

    SELECT *

    FROM temp

    This just gives me the two records which are root objects. It doesnt give me any child rows at all.

  • I think it shud be..

    WITH temp (Id, ParentId, iteration)

    AS

    (

    SELECT Id, ParentId, 0 AS iteration

    FROM OrgTable WHERE ParentId IS Null

    UNION ALL

    SELECT a.Id, b.ParentId, a.iteration + 1

    FROM temp a

    JOIN OrgTable b

    on b.ParentId= a.id

    )

    i have changed b.Id = a.ParentId to b.ParentId = a.id

    "Keep Trying"

Viewing 2 posts - 1 through 2 (of 2 total)

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