How to query for multiple parent-child records

  • Hi,

    I would like to query a parent record and all child records associated to the child records associated to the parent.

    ex. I have 2 child records, they each have 2 child records and so on.

    I would like to retrieve all child records in the tree. Can this be done without multiple cursors?

    I have a User table...the table has a UserID, a bunch of other user related columns and a SponsorID which holds the userID of the parent. I can retrieve my 2 child records but the additional child records are linked to my child records and so on. I don't know how many levels down I could go so I can't use a cursor...plus, if there are 5000 records or more, I can't have nested cursors executing multiple times.

    Is there a query that can retrieve me this information and not bring down a website?

    Thanks

  • In SQL 2005, this can be accomplished through the use of a recursive CTE.

    It sounds like you are building a Binary Tree, is that so?

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

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

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