Solved

How to do a hierarchical search on a tree structure

Posted on 2001-07-05
4
4,288 Views
Last Modified: 2008-04-16
How can I do a hierarchical search on a tree structure.. I currently have a table :

People:
EmployeeID
EmpName
EmpSurname
BossId

I want to retrieve all the employees that are under a certain Boss. For example the Employee that are right under him and all the employees that are under those employees, and etc..
As I understand with oracle they have a function called Connect By Prior And Start With... But How can i do this in MS SQL.. If I create a simple view, the number of nodes is unknown, since any employee can have their own employee. Please give me any example on how to do this, I am trying to plug this in into and asp application..
Thanks..
0
Comment
Question by:denz_1
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
nigelrivett earned 50 total points
ID: 6258012
need an sp

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

will give all the IDs.
0
 

Author Comment

by:denz_1
ID: 6259578
just wondering why do I need to create another table? What does it actualy do and what is the @@ sign.. Sorry I am still new in stored procedures..
0
 

Author Comment

by:denz_1
ID: 6259774
I have created a stored procedure:

Create Procedure treeSearch (@key int)

insert ti select(@key)

while @@rowcount > 0
insert ti
select empoloyeeID
from people, ti
where ti.id = people.bossID
and not exists (select * from ti ti2 where ti2.id = people.employeeID)

select * from ti


It stores everything into the ti table as desired, but it doesn't want to display the data using the (select * from ti).. It does not display or return anything. What is have I done wrong.
Thanks.

0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6260043
The @@ is used for system variables i.e. variables declared and maintained by the system.
Here it is used to find if any new values have been added to the table.

If select * from ti doesn't display anything how do you know there is anything in the table?

If you are accessing from a client then you may need a
set norowcount on
at the beginnnig to get the recordset.

ti is a permanent table so you probably need a
delete ti
to clear out old values from the table (that's why I used a temp table so it is created anew on every run).

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

770 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