473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

self join or stored procedure

hi gurus

have a employee table which consist of the normal employee related
fields which the fields empid and supervisor mainly related to this
question.

have to create a single (the question is whether it is possible) or a
stored procedure to get the result as follow. the input parameter is
empid.

have to retrieve the subordinates records of the employee id passed as
parameter and the drill down.

for example from the below table. if empid 12 is been passed as the
parameter value then it should retrieve the records with following
empid - 1, 26. the employee with empid 1 has subordinates under him
which should also be pulled that is 16, 62 and so on. the output for
the above example will be a result with records in the following order
- 12, 1,16,62, 26 and so on.

hope i have explained the problem clearly

would appreciate if some one can guide in creating an efficient self
join or a stored procedure
EmpId FirstName LastName Supervisor
----- --------- -------- ----------
1 Carl Hogans 12
12 Fred Smith NULL
16 Sue Bankers 1
26 Frank Green 12
55 Karen Feeders NULL
56 James Black 55
57 Kirk Simmons 56
58 Cliff Page 56
59 Jimmy Plant 56
60 Jack Cale 59
61 Robert Santana NULL
62 Jack Russell 1

where clause or parameter passed is 'EmpId'

thanx in advance

bala

Aug 3 '05 #1
4 3088
Look up the nested set model or get a copy of TREES & HIERARCHY IN SQL.

Aug 3 '05 #2
bala (ba*****@gmail.com) writes:
have a employee table which consist of the normal employee related
fields which the fields empid and supervisor mainly related to this
question.

have to create a single (the question is whether it is possible) or a
stored procedure to get the result as follow. the input parameter is
empid.

have to retrieve the subordinates records of the employee id passed as
parameter and the drill down.

for example from the below table. if empid 12 is been passed as the
parameter value then it should retrieve the records with following
empid - 1, 26. the employee with empid 1 has subordinates under him
which should also be pulled that is 16, 62 and so on. the output for
the above example will be a result with records in the following order
- 12, 1,16,62, 26 and so on.


In SQL 2000 you will have to write a stored procedure that recurses
through the hierarchy. You may find this section in Books Online
helpful: Access and Changing Relational Data / Advanded Query Concepts /
Transact-SQL Tips / Expanding Hierarchies.

In SQL 2005, currently in beta, there is a new T-SQL construct that
permits you to do this in a single query.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Aug 3 '05 #3
thanx --celko--

i am looking to the hierachial query from the example in books online
which deals with the worlds - states and cities.

was wondering whether there are any other approach.

thanx again

bala

Aug 3 '05 #4
thanx erland, much appreciated.

bala

Aug 3 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Berman | last post by:
Hi, I have a stored procedure that requires several input parameters. I would like to join the results of the stored procedure with another table but I'm having a tough time getting the syntax...
7
by: G.J. v.d. Kamp | last post by:
Hi all, I have a challenge (not a problem). Simplifing it as far as i could, i have this situation: Table Persons Id (PK) Name Table Scores
7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
10
by: Rada Chirkova | last post by:
Hi, at NC State University, my students and I are working on a project called "self-organizing databases," please see description below. I would like to use an open-source database system for...
2
by: btober | last post by:
In the command CREATE OPERATOR name ( PROCEDURE = func_name
3
by: Mike Fellows | last post by:
Help, im trying to use a union join from an SQL database and a Access database - the program works fine from my own machine as i have the odbc DSN connections setup from access to link to the...
4
by: Jack | last post by:
Hi all, While debugging some old code from someone, I came across this stored procedure: SELECT dbo.TBL_COORD.COORD_ID AS ID, dbo.TBL_COORD.LATITUDE AS Latitude, dbo.TBL_COORD.LONGITUDE AS...
0
by: Riaaaa | last post by:
Hello members, This is the SQL SERVER 2005 stored procedure. It contains the data from the two diff tables. (1)Customer : Here clpid and cardid are composite primary keys with the identity...
10
by: drweb | last post by:
i have made 2 views and concted them and my query is working find, but i want to make a 1 stored procedure from these 2 views ... View 1 SELECT dbo.page_information.page_id FROM ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.