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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:11:19+00:00 2026-05-14T07:11:19+00:00

I have a table in SQL that links to itself through parentID. I want

  • 0

I have a table in SQL that links to itself through parentID. I want to find the children and their children and so forth until I find all the child objects. I have a recursive function that does this but it seems very ineffective.

Is there a way to get sql to find all child objects? If so how?

Using: Microsoft SQL Server Management Studio Express 9.00.2047.00

  • 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-14T07:11:19+00:00Added an answer on May 14, 2026 at 7:11 am

    Have a look at using Sql Server 2005 CTEs.

    DECLARE @Table TABLE(
            ID INT,
            Val VARCHAR(10),
            ParentID INT
    )
    
    INSERT INTO @Table SELECT 1, 'A', NULL
    INSERT INTO @Table SELECT 2, 'B', NULL
    INSERT INTO @Table SELECT 3, 'C', 1
    INSERT INTO @Table SELECT 4, 'D', 1
    INSERT INTO @Table SELECT 5, 'E', 4
    INSERT INTO @Table SELECT 5, 'F', 2
    
    ;WITh Parents AS (
        SELECT  *,
                CAST(Val + '/'  AS VARCHAR(100))PathVal
        FROm    @Table
        WHERE   ParentID IS NULL
        UNION ALL
        SELECT  t.*,
                CAST(p.PathVal + t.Val + '/' AS VARCHAR(100))
        FROM    @Table t INNER JOIN 
                Parents p ON t.ParentID = p.ID
    )
    SELECT  *
    FROM    Parents
    

    Depending on the depth of the tree, you might want to take a look at

    MAXRECURSION query hint

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

Sidebar

Related Questions

I have a History Table in SQL Server that basically tracks an item through
On MS SQL Server 2005, I have a table that I want to replace.
I have a table in SQL server that has the normal tree structure of
I have a MS SQL table that I don't have any control over and
I have a table in a SQL Server 2005 database with a trigger that
I have a SQL statement that looks like: SELECT [Phone] FROM [Table] WHERE (
I have a table in the database that I'm retrieving using LINQ to SQL,
I have a table (T1) in t-sql with a column (C1) that contains almost
I have a rather large (many gigabytes) table of data in SQL Server that
Say I have an Oracle PL/SQL block that inserts a record into a table

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.