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 8255913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:41:29+00:00 2026-06-08T01:41:29+00:00

Given a child id, I need to return a query containing all parents of

  • 0

Given a child id, I need to return a query containing all parents of that child as well as their parents till I get to the root parent.
For example, given this data:

ID / Parent ID
1  /  0
2  /  1
3  /  2
4  /  0
5  /  3

So if I passed in ID 5 I would like to get a query with the results:

ID / Parent ID
1  /  0
2  /  1
3  /  2

This table does not work with a hierarchyid type so I suspect that this will need to be done with a CTE, but have no clue how. If it can be done in an SQL query / proc, any help would be appreciated.

Thanks

  • 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-06-08T01:41:31+00:00Added an answer on June 8, 2026 at 1:41 am

    This is more or less what you want:

    -- CTE to prepare hierarchical result set
    ;WITH #results AS
    (
        SELECT  id, 
                parentid 
        FROM    [table] 
        WHERE   id = @childId
        UNION ALL
        SELECT  t.id, 
                t.parentid 
        FROM    [table] t
                INNER JOIN #results r ON r.parentid = t.id
    )
    SELECT  *
    FROM    #results;
    

    Reference:

    • CTE: Common Table Expression

    Working example:

    -- create table with self lookup (parent id)
    CREATE TABLE #tmp (id INT, parentid INT);
    
    -- insert some test data
    INSERT INTO #tmp (id, parentid) 
    SELECT 1,0 UNION ALL SELECT 2,1 UNION ALL SELECT 3,2
    UNION ALL SELECT 4,0 UNION ALL SELECT 5,3;
    
    -- prepare the child item to look up
    DECLARE @childId INT;
    SET @childId = 5;
    
    -- build the CTE
    WITH #results AS
    (
        SELECT  id, 
                parentid 
        FROM    #tmp 
        WHERE id = @childId
        UNION ALL
        SELECT  t.id, 
                t.parentid 
        FROM    #tmp t
                INNER JOIN #results r ON r.parentid = t.id
    )
    
    -- output the results
    SELECT  * 
    FROM    #results 
    WHERE   id != @childId 
    ORDER BY id;
    
    -- cleanup
    DROP TABLE #tmp;
    

    Output:

    1 | 0
    2 | 1
    3 | 2

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

Sidebar

Related Questions

I need to be able to return a list of all children given a
In SQL Server 2008; I have a tree. I need to get all child
Given a simple parent/child class structure. I want to use linqkit to apply a
Given the following classes: @XmlRootElement(name = parent) class Parent { private Child child; //
I'm trying to create a procedure that would return a table with 'child numbers'
I'd like to create a LINQ query that returns the sum of all quantities
See title. Additionally, how can I tell if a given child element is only
FF and IE give differents offsetTop when a child is into a parent with
yes i want to get a child window of another application. Give me Sum
Given that the web application doesn't have su privileges, I'd like to execute 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.