Solved

Hierarchical Queries

Posted on 2001-06-09
2
627 Views
Last Modified: 2008-03-10
Hi

Does SQL Server 2000 supports hierarchical queries like Oracle.

Suppose I have a table

ID | PARENT ID | NAME
1  |           | A
2  |           | B
3  | 1         | C

I would like to select all records in this order:
1
3
2

With Oracle I could use connect by statement.

Saar
0
Comment
Question by:saar2
2 Comments
 
LVL 18

Accepted Solution

by:
nigelrivett earned 50 total points
Comment Utility
No but it is relatively easy to construct an SP to do this.

Heres one I did earlier

General way to handle trees. Fails when the squence varchar length is exceeded - in this case 100 levels.
Problems for more than 800.

create table #z (id int, name varchar(20), threadID int null)
insert #z select   1,  'foo', null
insert #z select     2,  'foo2',   null
insert #z select     3,  'foo2a',      2
insert #z select     4,  'foo2b',      2
insert #z select     5,  'foo3',   null
insert #z select     6,  'foo3b',      5
insert #z select     7,  'foo3ba',     6
insert #z select     8,  'foo4',   null
insert #z select     9,  'fooa',       1
insert #z select    10,  'foo2ba',     4

drop table #a
go
create table #a (id int, sequence varchar(1000), levelNo int)
insert #a select id, right(space(10) + convert(varchar(10),id),10), 1 from #z where threadid is null
declare @i int
select @i = 0
while @@rowcount > 0
begin
select @i = @i + 1
insert #a
select #z.id, sequence + right(space(10) + convert(varchar(10),#z.id),10), @i + 1
from #z, #a
where #a.levelNo = @i
and #z.threadID = #a.id
end

select #z.name
from #a, #z
where #a.id = #z.id
order by sequence
0
 
LVL 4

Author Comment

by:saar2
Comment Utility
This is yes/no question.

You got the points.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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

12 Experts available now in Live!

Get 1:1 Help Now