[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to do a hierarchical search on a tree structure

Posted on 2001-07-05
4
Medium Priority
?
4,320 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 200 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

834 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