Solved

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

Posted on 2007-11-28
6
441 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:spprivate
ID: 20368606
0
 
LVL 69

Accepted Solution

by:
ScottPletcher 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now