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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:32:55+00:00 2026-05-13T09:32:55+00:00

I have a table as shown below. ID ParentID Node Name Node Type ——————————————————————

  • 0

I have a table as shown below.

ID ParentID Node Name  Node Type
------------------------------------------------------------------
525 524  Root   Area Level 1
526 525  C   Area Level 2
527 525  A   Area Level 2
528 525  D   Area Level 2
671 525  E   Area Level 2
660 527  B   Area Level 3
672 671  F   Area Level 3

How can i write a recursive t-sql query to generate below output?

Output (“Root” node not required in the output):

Node  ID
-----------------------
A  527
A/B  660
C  526
D  528
E  671
E/F  672

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-05-13T09:32:55+00:00Added an answer on May 13, 2026 at 9:32 am

    Take a look at this page on using common table expressions. That is what I would use (assuming you are using at least SQL Server 2005)

    Here is a code example using your case:

     WITH CTE (NodePath, ID) AS (
        SELECT
            '/' + CAST(NodeName AS NVARCHAR(MAX)) AS NodePath,
            ID
        FROM TABLE
        WHERE NodeName = 'Root'
    
        UNION ALL
    
        SELECT
            CTE.NodePath + '/' + CAST(NodeName AS NVARCHAR(MAX)) AS NodePath,
            TABLE.ID
        FROM CTE
        INNER JOIN TABLE ON TABLE.ParentId = CTE.ID
    )
    
    SELECT
        NodeName,
        ID
    FROM CTE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table as shown below. It has accounts of type Fixed and
Ok I have a table in my mysql query browser like shown below: NAME:
Hello I have table name FriendsData that contains duplicate records as shown below fID
I have a table as shown below Q_ID DeptID EmployeeName City 100 100 testest
I do have a table data as shown below: <td> <label for=title>Title : </label>
I have a table withe different columns as shown below Ref Number Service Numbers
I have a SQLite table called posts . An example is shown below. I
I have created two tables & inserted values as shown below . Table 1
I have added checkbox in display table using decorator class as shown in below
I have Table MultiCol as below Name LibraryID RegisterID EngineerID Rahul 1002 4521 4854

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.