Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Recursion only with select

Author  Topic 

overthetop
Starting Member

18 Posts

Posted - 2006-06-01 : 05:34:25
Hi

I have a table (Id, Name, ManagerId)

Is it posible to return all the names in hierarchy order using a select statment? I hope someone can write down the query :)

10x

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-06-01 : 05:36:27
refer to here http://www.nigelrivett.net/RetrieveTreeHierarchy.html


KH

Go to Top of Page

overthetop
Starting Member

18 Posts

Posted - 2006-06-01 : 05:44:17
yes but this uses temp table. I mean only with select statment without dynamic sql. Something like this

select Id, Name

from .... join ...

... join ...

I really prefer using CTE but the reason I post this question is that today in the university gave us a task to do it and I think that the professor was joking
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-06-01 : 06:04:02
Is it unlimited no of levels or if it is limit to few you can use inner join to join back the same table


KH

Go to Top of Page

overthetop
Starting Member

18 Posts

Posted - 2006-06-01 : 06:06:39
The number of levels is unlimited. That is the problem
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-06-01 : 06:11:36
Yes it is really a long problem


KH

Go to Top of Page

overthetop
Starting Member

18 Posts

Posted - 2006-06-01 : 06:12:51
xex
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-01 : 07:40:14
quote:
Originally posted by overthetop

yes but this uses temp table. I mean only with select statment without dynamic sql. Something like this

select Id, Name

from .... join ...

... join ...

I really prefer using CTE but the reason I post this question is that today in the university gave us a task to do it and I think that the professor was joking


Read Nigel's approach. There is no dynamic SQL. You cant avoid having temp table if the level is unlimited

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

peterlemonjello
Yak Posting Veteran

53 Posts

Posted - 2006-06-01 : 08:29:23
If you're using SQL 2005 use a CTE
Go to Top of Page
   

- Advertisement -