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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:57:09+00:00 2026-05-17T01:57:09+00:00

I have a table like this: Item { int ItemID int ParentID string Name

  • 0

I have a table like this:

Item
{
   int ItemID
   int ParentID 
   string Name
}

Item is actually a subset of a larger table Object:

Object
{
    int ObjectID
    string Color
}

So ItemID is an FK to ObjectID. Within Item, ParentID can either refer to another Item or to the parent Object.

What I’d like to do is be able to iterate from a leaf in the Item relationship all the way up through its parents until I finally can determine what ObjectID a given Item leaf descends from.

I’d like to do this in SQL. I’m using SQL Server 2008.

Here’s what I’m thinking. I can just iterate up through the the Item ParentID until I can no longer join ParentID with another Item. This ParentID is the ObjectID I want to return.

I’ve been trying to get this to work using inner joins, but no luck. I’m using C# so if this can be done in linq, which i don’t suspect it can without being horribly inefficient, that would be fine too.

Answer I went With:

WITH ObjectH (ParentID, ItemID, Level) AS
(
   -- Base case
   SELECT
      ParentID,
      ItemID,
      1 as Level 
   FROM Item
   WHERE ItemID = @param 

   UNION ALL

   -- Recursive step
   SELECT
      i.ParentID,
      i.ItemID,
      oh.Level + 1 AS Level
   FROM Item i
      INNER JOIN ObjectH oh ON
         c.ItemID = oh.ParentID           
)

SELECT TOP 1 ParentID
FROM ObjectH
ORDER BY Level DESC

This works.

  • 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-17T01:57:10+00:00Added an answer on May 17, 2026 at 1:57 am

    My attempt at trying to do this with a Recursive SQL Server CTE.

    Logically, this is how CTE works and this should but I have never used it with a schema exactly like yours.

    Usually a tree structure in SQL doesnt have the additional parent “Object” table. (Seems like you are mimicking your objects design exactly 1:1 in the db as well?. I would relook that pattern if i were you )

    Try it and let me know if it works.

    EDIT: Changed the base query condition a bit

    EDIT2: Changed the query to make it specific for one item

    WITH ObjectHierarchy (ItemID, Name, ParentID, Level) AS
    (
       SELECT 
          ItemID, 
          Name, 
          ParentID, 
          1 as Level 
       FROM Item it, Object ob
       WHERE it.ParentID = ob.ObjectID
       AND ob.ItemID = @itemIdToBeSearched
    
       UNION ALL
    
       SELECT
          i.ItemID,
          i.Name,
          i.ParentID,
          oh.Level + 1 AS Level
       FROM Item i
       INNER JOIN ObjectHierarchy oh ON
          i.ParentID = oh.ItemID
       AND oh.ItemID = @itemIdToBeSearched
    
    )
    
    SELECT parentID
    FROM ObjectHierarchy 
    WHERE LEVEL = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.