?
Solved

microsoft, sql, server, 2000 database ,get top parent in query

Posted on 2007-11-28
6
Medium Priority
?
458 Views
Last Modified: 2008-02-01
folks

i have a parent child relationship in sql server

  child   parent
  aaa      aa
  aa        a
   a          

how if I select child aaa can I display a as its top parent i.e

child    top parent
aaa        a

all help will do,i use the connect by prior in oracle but havent found anyhting decent in sql
       
0
Comment
Question by:rutgermons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:spprivate
ID: 20368542
This is a solution to another question in the same lines
create table #a (id int)
insert #a select @bossID
 
while @@rowcount > 0
insert #a 
select empoloyeeID
from people, #a
where #a.id = people.bossID
and not exists (select * from #a a2 where a2.id = people.employeeID)
 
select * from #a

Open in new window

0
 
LVL 15

Expert Comment

by:spprivate
ID: 20368551
Create an stored procedure. with the above code
0
 

Author Comment

by:rutgermons
ID: 20368558
could yoou post me the entire link pls?
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 1500 total points
ID: 20369698
Instead use a function so that you can call it in-line as part of a SELECT; for example:

CREATE FUNCTION dbo.GetTopParent (
    @child VARCHAR(10)
)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @parent VARCHAR(10)

WHILE 1 = 1
BEGIN
    SELECT @parent = parent
    FROM tableName
    WHERE child = @child
    IF ISNULL(@parent, '') = ''
        BREAK
    SET @child = @parent
END --WHILE

RETURN @child
END --FUNCTION


SELECT child, dbo.GetTopParent(child) AS TopParent
FROM tableName
0
 
LVL 52

Expert Comment

by:_agx_
ID: 20370497
(No points)

Whatever method you choose,  be certain it prevents infinite loops from occurring.  For example if an error in your data created an unintentional circular reference like this

child, parent
'aaaa', 'aaa'
'aaa', 'aa'   <--- circular reference
'aa', 'aaa'   <--- circular reference
'a', NULL

It could cause an infinite loop here

WHILE 1 = 1
BEGIN
    SELECT @parent = parent
    FROM tableName
    WHERE child = @child
    IF ISNULL(@parent, '') = ''
        BREAK
    SET @child = @parent
END --WHILE
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question