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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:46:34+00:00 2026-05-16T23:46:34+00:00

We have a table where rows recursively link to another row. I want to

  • 0

We have a table where rows recursively link to another row. I want to pull data associated with a given parentId and all it’s children. Where parentId is one from the root row.

I thought I have seen or done something like that before, but I am unable to find it now. Can this be done in SQL or is it better to do this in code?

I want the list to look like this when I’m done:

  • Parent
    • Child
      • Grandchild
  • 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-16T23:46:34+00:00Added an answer on May 16, 2026 at 11:46 pm

    This can be done in SQL Server 2005 and above using Common Table Expressions (CTEs). Here is a great link from MSDN describing recursive queries: Recursive Queries Using Common Table Expressions

    Here is an example:

    If you imagine a hierarchical line of people, this query will let you see the complete line of any person AND calculates their place in the hierarchy. It can be modified to find any child relationship.

    Instead of the ID of the person, you swap in the ID of the row you are using as your parent.

    --Create table of dummy data
    create table #person (
    personID integer IDENTITY(1,1) NOT NULL,
    name      varchar(255) not null,
    dob       date,
    father    integer
    );
    
    INSERT INTO #person(name,dob,father)Values('Pops','1900/1/1',NULL);  
    INSERT INTO #person(name,dob,father)Values('Grandma','1903/2/4',null);
    INSERT INTO #person(name,dob,father)Values('Dad','1925/4/2',1);
    INSERT INTO #person(name,dob,father)Values('Uncle Kev','1927/3/3',1);
    INSERT INTO #person(name,dob,father)Values('Cuz Dave','1953/7/8',4);
    INSERT INTO #person(name,dob,father)Values('Billy','1954/8/1',3);
    
    DECLARE @OldestPerson INT; 
    SET @OldestPerson = 1; -- Set this value to the ID of the oldest person in the family
    
    WITH PersonHierarchy (personID,Name,dob,father, HierarchyLevel) AS
    (
       SELECT
          personID
          ,Name
          ,dob
          ,father,
          1 as HierarchyLevel
       FROM #person
       WHERE personID = @OldestPerson
    
       UNION ALL
    
       SELECT
        e.personID,
          e.Name,
          e.dob,
          e.father,
          eh.HierarchyLevel + 1 AS HierarchyLevel
       FROM #person e
          INNER JOIN PersonHierarchy eh ON
             e.father = eh.personID
    )
    
    SELECT *
    FROM PersonHierarchy
    ORDER BY HierarchyLevel, father;
    
    DROP TABLE #person;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table with few rows, after use hover mouse over row I want
I have a table with multiple rows. Each row is a form. I want
I have table rows of data in html being filled from a CGI application.
how remove html table rows after 2nd row? if in table have 5 row,
I have a table with multiple rows in each row is a select list
I have table which has a class data and it has table rows inside
in my app, i have created table rows dynamically in each table row, i
I want to have a table with clickable rows that upon clicking will redirect
I have a table, which I want to split into rows of x, which
I have a table with well over 5 millions rows, that contains hierarchical data

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.