Solved

How to do a hierarchical search on a tree structure

Posted on 2001-07-05
4
4,298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

742 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