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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:18:07+00:00 2026-05-17T15:18:07+00:00

I have table with ID who is PK and I have in same table

  • 0

I have table with ID who is PK and I have in same table column ParentID who is in some cases ID’s.(I say some cases because for example ParentID can be 0 but this ID not have in ID’s column).
I try to create Query:

ID     ParentID     Value
1      0            Test
2      0            Test01
3      2            Test02
4      3            Test03

And when I send ID=4 I need this result:

ID     ParentID     Value
2      0            Test01
3      2            Test02
4      3            Test03

Any help…

  • 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-17T15:18:08+00:00Added an answer on May 17, 2026 at 3:18 pm

    This looks like a tree traversal problem to me, which doesn’t lend itself particularly well to set-based operations. The following works, but it’s not too elegant:

    -- turn this into stored proc?
    declare @inputId int
    set @inputId = 4
    
    
    declare @Ids table (ID int)
    insert into @Ids values (@inputId)
    
    declare @reccount int
    set @reccount = 1
    declare @lastreccount int
    set @lastreccount = 0
    while @reccount <> @lastreccount
    begin
        set @lastreccount = @reccount
        insert into @Ids 
            select ParentID from recursiveTest 
                where ID in (select ID from @Ids)
                    and ParentID not in (select ID from @Ids)
        set @reccount = (select COUNT(*) from @Ids)                      
    end
    
    select * from recursiveTest where ID in (select ID from @Ids);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a two table called A and B both have same structure but
I have been reading about this but I have to do some thing. I
I have a table that looks like this: CREATE TABLE [dbo].[StudentTime] ([StudentTimeID] [int] NOT
I have a large table with 1 million+ records. Unfortunately, the person who created
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table with more than a millon rows. This table is used
I have a secondhand sale car database and four tables with relationships. Same column
I have a table of events on a page. Last column contains available actions,
I have a table storing millions of rows. It looks something like this: Table_Docs
I'm pretty sure I've seen this somewhere, but I can't find the right terminology

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.