jpsteiner
asked on
How can I Retrieve a hierarchical Recordset from a MSSQL stored procedure?
I try to generate a hierarchical Tree with a stored procedure an pass the result to a ADO Recordset.
The Database is quite easy, each Employee has a ID (MaKurz), Name (MaName), Status (MaStatus), Deputy (MaStv) and a Chef (MaChef). What I need is a recordset whith all Emloyees (and sub-employees) of a specific manager.
I've found a answere here in EE, which was partially helpful. Here is my Version which is still working with Query Analyzer but gives just the key (MaKurz) and only in Query Analyzer.
CREATE PROC dbo.ShowHierarchy
(
@Root varchar(6)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @MaKurz varchar(6), @MaName varchar(30), @MaStatus int
PRINT @Root
SET @MaKurz = (SELECT MIN(MaKurz) FROM dbo.T250_Ma WHERE Vorgesetzter = @Root)
WHILE @MaKurz IS NOT NULL
BEGIN
EXEC dbo.ShowHierarchy @MaKurz
SET @MaKurz = (SELECT MIN(MaKurz) FROM dbo.T250_Ma WHERE Vorgesetzter = @Root AND MaKurz > @MaKurz)
END
END
GO
Well, the problem is that I can't receive the List in a VB ADO Recordset.
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "ShowHierarchy"
cmd.Parameters.Append cmd.CreateParameter("@Root ", adBSTR, adParamInput, , "xyz")
Set cmd.ActiveConnection = db
Dim rst As ADODB.Recordset
Set rst = cmd.Execute
MsgBox rst.RecordCount
rst.Close
The second problem is that I need also the other informations like Name and Status of the Employees, but for this I could start a new Query in ADO.
The Database is quite easy, each Employee has a ID (MaKurz), Name (MaName), Status (MaStatus), Deputy (MaStv) and a Chef (MaChef). What I need is a recordset whith all Emloyees (and sub-employees) of a specific manager.
I've found a answere here in EE, which was partially helpful. Here is my Version which is still working with Query Analyzer but gives just the key (MaKurz) and only in Query Analyzer.
CREATE PROC dbo.ShowHierarchy
(
@Root varchar(6)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @MaKurz varchar(6), @MaName varchar(30), @MaStatus int
PRINT @Root
SET @MaKurz = (SELECT MIN(MaKurz) FROM dbo.T250_Ma WHERE Vorgesetzter = @Root)
WHILE @MaKurz IS NOT NULL
BEGIN
EXEC dbo.ShowHierarchy @MaKurz
SET @MaKurz = (SELECT MIN(MaKurz) FROM dbo.T250_Ma WHERE Vorgesetzter = @Root AND MaKurz > @MaKurz)
END
END
GO
Well, the problem is that I can't receive the List in a VB ADO Recordset.
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "ShowHierarchy"
cmd.Parameters.Append cmd.CreateParameter("@Root
Set cmd.ActiveConnection = db
Dim rst As ADODB.Recordset
Set rst = cmd.Execute
MsgBox rst.RecordCount
rst.Close
The second problem is that I need also the other informations like Name and Status of the Employees, but for this I could start a new Query in ADO.
ASKER
It is MSSQL V2k
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
angellll:
When I try to create the function I get the following Errormessage:
Invalid column name 'MaMurz'
Invalid use of 'SET OPTION ON' within a function.
should I replace 'MaMurz' with 'MaKurz' (3 times)
should I remove 'SET NOCOUNT ON'?
Thank you for supporting!
When I try to create the function I get the following Errormessage:
Invalid column name 'MaMurz'
Invalid use of 'SET OPTION ON' within a function.
should I replace 'MaMurz' with 'MaKurz' (3 times)
should I remove 'SET NOCOUNT ON'?
Thank you for supporting!
>should I replace 'MaMurz' with 'MaKurz' (3 times)
>should I remove 'SET NOCOUNT ON'?
yes, sorry for those errors...
>should I remove 'SET NOCOUNT ON'?
yes, sorry for those errors...
ASKER
the correct vb syntax is:
select * from dbo.GetHierarchy('abc') as l join T250_Ma as t on t.MaKurz = l.MaKurz
Thank you anyway!
select * from dbo.GetHierarchy('abc') as l join T250_Ma as t on t.MaKurz = l.MaKurz
Thank you anyway!
you can, if you check the rst.NextRecordset object (recurring), each SELECT of the procedure will generate an additional .NextRecordset object.
now, what version of MS SQL Server are you using?