Information générale
Forum:
Microsoft SQL Server
Titre:
Hierarchical information
Hello
I have a table named personnel which is defined as below :
create table personnel (
cuid char(10),
cname char(45),
cmanagerid char(10)
)
Sample data is as follows :
1111111111,Mike,2222222222
3333333333,John,1111111111
2222222222,Glen,NULL
Cuid is the unique identification character key which is generated by an indigeneous key generating engine.
cmanagerid is the foreign key linked to personnel.cuid.
What I want to do is as follows :
I want to write an sql statement which returns me a cursor describing the hierarchy of this table. The parameters to be passed are the starting cuid and the number of levels.
For example :
if the parameters are cuid = 3333333333 and level = 3 then cursor must be :
column1 column2 column3
John Mike Glen
or
column1
John
Mike
Glen
if the parameters are cuid = 3333333333 and level = 2 then cursor must be :
column1 column2
John Mike
or
column1
John
Mike
if the parameters are cuid = 1111111111 and level = 2 then cursor must be :
column1 column2
Mike Glen
or
column1
Mike
Glen
or
column1
Mike
Glen
Null
I hope the problem is clear. Is there a way to achive this directly using a sql statement and not using a stored procedure.
However, solutions in both the ways (stored procedure and sql statement) are welcome.
Thanks in anticipation.
Ajay
Suivant
Répondre
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement