Solved

How to do a hierarchical search on a tree structure

Posted on 2001-07-05
4
4,296 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table where row act as column 11 68
SQL Query help 3 24
SQL Select Query help 1 34
how to extract last 6 digits from a varchar, sql 4 19
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 …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

713 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