Solved

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

Posted on 2007-11-28
6
450 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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 15

Expert Comment

by:spprivate
ID: 20368606
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 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