Solved

How to do a hierarchical search on a tree structure

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how the fundamental information of how to create a 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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now