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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:42:57+00:00 2026-06-12T21:42:57+00:00

I have many similar structure tables like this: CREATE TABLE [dbo].[tbl_Hierarchy]( [ID] [int] NOT

  • 0

I have many similar structure tables like this:

CREATE TABLE [dbo].[tbl_Hierarchy](
[ID] [int]  NOT NULL,
[ParentID] [int] NOT NULL,
[Text] [nvarchar](100)  NOT NULL,
--other field irrelevant to my question
)

INSERT INTO dbo.tbl_Hierarchy VALUES(1,0,'parent1')
INSERT INTO dbo.tbl_Hierarchy VALUES(2,0,'parent2')
INSERT INTO tbl_Hierarchy VALUES(3,1,'child1')
INSERT INTO tbl_Hierarchy VALUES(4,3,'grandchild1')
INSERT INTO  tbl_Hierarchy VALUES(5,2,'child2')

Can you help me writing such as a stored procedure including two parameters with table name and ID ?

For example, when executing

EXEC usp_getChildbyID  tbl_Hierarchy, 1

the result set should be:

ID  Text        Level
1   parent1      1
3   child1       2
4   grandchild1  3

Thanks a lot in advance.

  • 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-12T21:42:59+00:00Added an answer on June 12, 2026 at 9:42 pm

    This recursive CTE should do the trick.

    WITH RecursiveCte AS
    (
        SELECT 1 as Level, H1.Id, H1.ParentId, H1.Text FROM tbl_Hierarchy H1
        WHERE id = @Id
        UNION ALL
        SELECT RCTE.level + 1 as Level, H2.Id, H2.ParentId, H2.text FROM tbl_Hierarchy H2
        INNER JOIN RecursiveCte RCTE ON H2.ParentId = RCTE.Id
    )
    SELECT Id, Text, Level FROM RecursiveCte
    

    If you really want it with a dynamic table in a procedure this could be a solution

    CREATE PROCEDURE usp_getChildbyID
        @TableName nvarchar(max),
        @Id int
    AS
    BEGIN
    
        DECLARE @SQL AS nvarchar(max)
        SET @SQL = 
        'WITH RecursiveCte AS
        (
            SELECT 1 as Level, H1.Id, H1.ParentId, H1.Text FROM ' + @TableName + ' H1
            WHERE id = ' + CAST(@Id as Nvarchar(max)) + '
            UNION ALL
            SELECT RCTE.level + 1 as Level, H2.Id, H2.ParentId, H2.text FROM ' + @TableName + ' H2
            INNER JOIN RecursiveCte RCTE ON H2.ParentId = RCTE.Id
        )
        select Id, Text, Level from RecursiveCte'
    
        EXEC sp_executesql @SQL;
    END
    

    Edit:

    Sql fiddle example: http://sqlfiddle.com/#!3/d498b/22

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

Sidebar

Related Questions

This is probably not a dup; I have read through many similar problems on
I have a MySQL server with many databases that have a similar table structure.
my issue lokks similar to this one: (link) but i have one-to-many association: <set
I have a file that is similar to this: <many lines of stuff> SUMMARY:
I have a data structure similar to the following picture. Table A has_many Table
I have found many similar posts and even tried to find out how to
I have many controllers that will have some similar behavior, e.g. the user should
I have similar instances of the same web application running across many domains. I
I know similar questions have been asked before, but I've tried many times and
This is my fort post on stackoverflow. I have searched many similiar Q&A's on

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.