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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:15:55+00:00 2026-06-13T18:15:55+00:00

I have a database with a table called Items, that contains these columns: ID

  • 0

I have a database with a table called Items, that contains these columns:

  • ID – primary key, uniqueidentifier
  • Name – nvarchar(256)
  • ParentID – uniqueidentifier

The name field can be used to build out a path to the item, by iterating through each ParentId until it equals ‘11111111-1111-1111-1111-111111111111’, which is a root item.

So if you had a table that had rows like

ID                                   Name        ParentID
-------------------------------------------------------------------------------------
11111111-1111-1111-1111-111111111112 grandparent 11111111-1111-1111-1111-111111111111
22222222-2222-2222-2222-222222222222 parent      11111111-1111-1111-1111-111111111112
33333333-3333-3333-3333-333333333333 widget      22222222-2222-2222-2222-222222222222

So if I looked up an item with id ‘33333333-3333-3333-3333-333333333333’ in the example above, i’d want the path

/grandparent/parent/widget 

returned. i’ve attempted to write a CTE, as it looks like that’s how you’d normally accomplish something like this – but as I don’t do very much SQL, I can’t quite figure out where i’m going wrong. I’ve looked at some examples, and this is as close as I seem to be able to get – which only returns the child row.

declare @id uniqueidentifier
set @id = '10071886-A354-4BE6-B55C-E5DBCF633FE6'
;with ItemPath as (
    select a.[Id], a.[Name], a.ParentID 
        from Items a
            where Id = @id

    union all

    select parent.[Id], parent.[Name], parent.ParentID 
        from Items parent 
            inner join ItemPath as a
                on a.Id = parent.id
                    where parent.ParentId = a.[Id]
)
select * from ItemPath

I have no idea how i’d declare a local variable for the path and keep appending to it in the recursive query. i was going to try to at least get all the rows to the parent before going after that. if anyone could help with that as well – i’d appreciate it.

  • 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-13T18:15:56+00:00Added an answer on June 13, 2026 at 6:15 pm

    well here’s working solution

    SQL FIDDLE EXAMPLE

    declare @id uniqueidentifier
    set @id = '33333333-3333-3333-3333-333333333333'
    
    ;with ItemPath as 
    (
        select a.[Id], a.[Name], a.ParentID 
        from Items a
        where Id = @id
    
        union all
    
        select parent.[Id], parent.[Name] + '/' + a.[Name], parent.ParentID 
        from ItemPath as a
            inner join Items as parent on parent.id = a.parentID
    )
    select * 
    from ItemPath
    where ID = '11111111-1111-1111-1111-111111111112'
    

    I don’t like it much, I think better solution will be to do it other way around. Wait a minute and I try to write another query 🙂

    UPDATE here it is

    SQL FIDDLE EXAMPLE

    create view vw_Names
    as
        with ItemPath as 
        (
            select a.[Id], cast(a.[Name] as nvarchar(max)) as Name, a.ParentID 
            from Items a
            where Id = '11111111-1111-1111-1111-111111111112'
    
            union all
    
            select a.[Id], parent.[Name] + '/' + a.[Name], a.ParentID 
            from Items as a
                inner join ItemPath as parent on parent.id = a.parentID
        )
    select * 
    from ItemPath
    

    and now you can use this view

    declare @id uniqueidentifier
    set @id = '33333333-3333-3333-3333-333333333333'
    
    select * 
    from vw_Names where Id = @id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Database: Table: news With the columns: Unique and Primary Key which auto increments called
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a database table with a column called 'symbol', that is unique via
Say I have a database called Poll and have these two table structures. poll
Learning php! I have a sql database which contains a table called 'images' which
I have a MySQL database with a table called subm_items The table contains the
Given a Database that has a table Called [Bid Transactions], which contains records of
I have a table in my SQL Server database called items which has a
I have a table in my MySQL database called 'children'. In that table is
I have an items table in my MySQL database with a DECIMAL(10,2) field called

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.