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

  • Home
  • SEARCH
  • 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 6028891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:48:23+00:00 2026-05-23T04:48:23+00:00

I have a table that contains the following data: +—-+———-+ | ID | ParentID

  • 0

I have a table that contains the following data:

+----+----------+
| ID | ParentID |
+----+----------+
| 27 |    0     |
| 38 |    27    | 
| 45 |    38    |
| 86 |    0     |
| 92 |    45    |
| 48 |    86    |
| 62 |    92    |
| 50 |    62    |
-----------------

I would like to be able to pass any ID to a stored procedure and get the entire chain of IDs (parents and children) of that given ID.

ie. if I pass ID = 45, I should get:

27
38
45
92
62
50

Similarly, if I pass ID = 86, I should get:

86
48

Any help would be greatly appreciated!

  • 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-23T04:48:23+00:00Added an answer on May 23, 2026 at 4:48 am

    You can use two recursive CTE’s. The first finds the root node and the second builds the chain.

    declare @T table(ID int, ParentID int)
    
    insert into @T values (27,  0), (38, 27), (45, 38), (86,  0),
                          (92, 45), (48, 86), (62, 92), (50, 62)    
    
    declare @ID int = 45
    
    ;with cte1 as
    (
      select T.ID, T.ParentID, 1 as lvl
      from @T as T
      where T.ID = @ID
      union all
      select T.ID, T.ParentID, C.lvl+1
      from @T as T
        inner join cte1 as C
          on T.ID = C.ParentID
    ),
    cte2 as
    (
      select T.ID, T.ParentID
      from @T as T
      where T.ID = (select top 1 ID
                    from cte1
                    order by lvl desc)
      union all
      select T.ID, T.ParentID
      from @T as T
        inner join cte2 as C
          on T.ParentID = C.ID
    )
    select ID
    from cte2
    

    Version 2

    A bit shorter and query plan suggests more effective but you never know without testing on real data.

    ;with cte as
    (
      select T.ID, T.ParentID, ','+cast(@ID as varchar(max)) as IDs
      from @T as T
      where T.ID = @ID
      union all
      select T.ID, T.ParentID, C.IDs+','+cast(T.ID as varchar(10))
      from @T as T
        inner join cte as C
          on (T.ID = C.ParentID or
              T.ParentID = C.ID) and
              C.IDs+',' not like '%,'+cast(T.ID as varchar(10))+',%'
    )
    select ID
    from cte
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is for MySQL and PHP I have a table that contains the following
I have a database that contains a table that looks a bit like this:
I have a table that contains the following: DataDate Value 2010-03-01 08:31:32.000 100 2010-03-01
I have a table that contains some meta data in an XML field. For
I have a component called a TableDataViewer that contains the following pieces of data
I have a table that contains tasks and I want to give these an
I have a table that contains maybe 10k to 100k rows and I need
I have a table that contains some blob fields that I don't want to
I have a products table that contains a FK for a category, the Categories
I have an SQLite table that contains a BLOB I need to do a

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.