Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-28
6
Medium Priority
?
460 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 70

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

618 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