Solved

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

Posted on 2007-11-28
6
456 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

696 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