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
 SQL Server Development (2000)
 tree structure`

Author  Topic 

xpandre
Posting Yak Master

212 Posts

Posted - 2002-01-31 : 10:16:51
Hi guys,

I have a table such


id refid
1 0
2 0
3 1
4 3
5 3
6 4
7 1
8 1
9 7
10 1

Now i want all subordinates for 3 ,...such

id refid
3 1
4 3
5 3
6 4


Can this be done via a single query?
if not,has any1 implemented a fucntion or proc for this...i need it very urgent ..and i think i wont have enough time to write a stored proc now .
Thank you
Sam

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-01-31 : 10:30:07
Can anyone explain the anti-stored procedure sentiment going around today?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=12515

Have you looked at graz's articles on the threaded discussions?

http://www.sqlteam.com/item.asp?ItemID=1353
http://www.sqlteam.com/item.asp?ItemID=1602

For what it's worth, with the structure you have you'll need to have some kind of loop to fully traverse the tree. If you need to do this from a specific node, then making it a stored procedure will be easy: just pass the parent node in and it will zip right through.

Go to Top of Page

xpandre
Posting Yak Master

212 Posts

Posted - 2002-01-31 : 10:53:02
thanx for the reply...
1 more question
How do i retrive just 1 row in the above table...with no asc/desc conditions?
Thank you
Sam

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-01-31 : 11:23:07
You can use TOP:

SELECT TOP 1 * FROM myTable

Or you can SET ROWCOUNT:

SET ROWCOUNT 1
SELECT * FROM myTable


If you use SET ROWCOUNT and you have a batch of commands, make sure you SET ROWCOUNT immediately before the SELECT statement. If you SET ROWCOUNT before an UPDATE or DELETE it will limit the number of rows affected by those operations.

Go to Top of Page
   

- Advertisement -