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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:37:38+00:00 2026-05-30T16:37:38+00:00

The select * from MyTable gives the following result, Now I would like to

  • 0

The select * from MyTable gives the following result,
enter image description here

Now I would like to get ids of all childs if I pass the id of ‘Chest’.

For example, if I pass id of ‘ChestOne’, I would like to get the following table

enter image description here

I tried the following, but could not get the result

;WITH Temp_CTE AS
(
SELECT 
     id
    ,name
    ,parent_id
FROM dbo.MyTable
WHERE id = '12D390AE-FF28-4D50-B3A2-A6CC4E6E02A9'
UNION ALL 
SELECT 
     CS.id
    ,CS.name
    ,CS.parent_id
FROM dbo.MyTable CS
INNER JOIN Temp_CTE ON CS.id =  Temp_CTE.parent_id

)
SELECT * FROM Temp_CTE

How can I do it in SQL Server 2008 ?

  • 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-30T16:37:40+00:00Added an answer on May 30, 2026 at 4:37 pm
    declare @MyTable table 
    (
      id char(2),
      name varchar(10),
      parent_id char(2)
    )  
    
    insert into @MyTable values
    ('54', 'Chest Two', '12'),
    ('CD', 'InCOne', '6D'),
    ('6D', 'ChestOne', '12'),
    ('12', 'Chest', '00')
    
    ;WITH Temp_CTE AS
    (
    SELECT 
         id
        ,name
        ,parent_id
    FROM @MyTable
    WHERE id = '6D'
    UNION ALL 
    SELECT 
         CS.id
        ,CS.name
        ,CS.parent_id
    FROM @MyTable CS
    INNER JOIN Temp_CTE ON CS.parent_id =  Temp_CTE.id
    
    )
    SELECT * FROM Temp_CTE
    

    Result:

    id   name       parent_id
    ---- ---------- ---------
    6D   ChestOne   12
    CD   InCOne     6D
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SELECT Name1, Name2, Value FROM mytable gives me the following result set: Name1 Name2
SELECT * FROM myTable WHERE field1 LIKE 'match0' AND myfunc(t1.hardwareConfig) LIKE 'match1' Here is
I have a query like SELECT * FROM myTable WHERE key LIKE 'XYZ' The
I have a site with a SELECT statement like this: SELECT * FROM MyTable
I have a query that looks like this SELECT * from myTable WHERE Date
Given following snippet (MS SQL): DECLARE UpdateList CURSOR FOR SELECT MyColumn FROM MyTable OPEN
I have the following query. SELECT COUNT(*), WEEK(date), YEAR(date) FROM myTable GROUP ON YEAR(date),
Given the following query: Select * from myTable where stringField in ('A','B','C') I'll be
Given following Table select rv, mv, datetime, rawv from mytable where datetime > '2012-01-30';
I have a query of the following form: SELECT * FROM MyTable WHERE Timestamp

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.