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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:12:54+00:00 2026-06-10T06:12:54+00:00

In SQL server Ok, so I’m working with a database table in which rows

  • 0

In SQL server

Ok, so I’m working with a database table in which rows can have parent rows, which can then have parent rows of their own. I need to select the root ‘row’. I don’t know the best way to do this.

There is a field called ParentId, which links the row to the row with that ID. When the ParentId = 0, it is the root row.

This is my query now:

SELECT Releases.Name,WorkLog.WorkLogId 

FROM WorkLog,Releases
WHERE
Releases.ReleaseId = WorkLog.ReleaseId
and WorkLogDateTime >= @StartDate
and WorkLogDateTime <= @end

I don’t really need the Release Name of the child releases, I want only the root Release Name, so I want to select the result of a While loop like this:

WHILE (ParentReleaseId != 0)
BEGIN
@ReleaseId = ParentReleaseId
END

Select Release.Name
where Release.RealeaseId = @ReleaseId

I know that syntax is horrible, but hopefully I’m giving you an idea of what I’m trying to acheive.

  • 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-10T06:12:55+00:00Added an answer on June 10, 2026 at 6:12 am

    Here is an example, which could be usefull:

    This query is getting a lower element of a tree, and searching up to the parent of parents.
    Like I have 4 level in my table -> category 7->5, 5->3, 3-> 1. If i give it to the 5 it will find the 1, because this is the top level of the three.

    (Changing the last select you can have all of the parents up on the way.)

    DECLARE @ID int
    
    SET @ID = 5;
    
    WITH CTE_Table_1
    (
      ID,
      Name,
      ParentID
    )
    AS(
      SELECT 
       ID,
       Name,
       ParentID
      FROM Table_1
      WHERE ID = @ID
    
     UNION ALL
    
     SELECT 
      T.ID,
      T.Name,
      T.ParentID
     FROM Table_1 T
     INNER JOIN CTE_Table_1 ON CTE_Table_1.ParentID = T.ID
    )
    
    SELECT * FROM CTE_Table_1 WHERE ParentID = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL Server 2005 I have an SQL Function (ftn_GetExampleTable) which returns a table with
SQL Server 2000 I have a table which lists out the months where a
SQL Server 2005 I have a table which returns ID name prop value --------------------------
SQL Server procedure can return result sets. I have a table emp(emp__id, emp__name, ...)
SQL Server 2008 Database Question. I have 2 tables, for arguments sake called Customers
SQL Server 2008. I have this very large query which has a high cost
SQL Server 2005. I have a stored procedure which accepts a few parameters that
SQL SERVER, I'm working with a table that uses a guid for the key
SQL Server 2005 I have 10 million rows in DB, and run a select
SQL Server express editions have memory limit of 1GB. Can I install multiple versions

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.