Newbie Q: Multiple Recordsets Returned as One in SP

Newbie Q: Multiple Recordsets Returned as One in SP

Post by Gary Lill » Wed, 22 May 2002 03:52:21



Forgive my ignorance.  I want to pass a few variables to a stored procedure,
ask the stored procedure to run three different SELECT statements (with a
single value each), then return the results of all three as one resultset to
be used with an ASP page.

My attempt is shown below.  While this works in Query Analyzer, it does so
by returning three separate recordsets:

-----------------------------------------

CREATE PROCEDURE userSP_sysTeacherAttainMasteryCount



AS

SELECT     COUNT(*) AS RedMasteryCount
FROM         tblProgress INNER JOIN
                       tblEnrollment ON tblProgress.StudentID =
tblEnrollment.StudentID

SELECT     COUNT(*) AS YellowMasteryCount
FROM         tblProgress INNER JOIN
                       tblEnrollment ON tblProgress.StudentID =
tblEnrollment.StudentID


SELECT     COUNT(*) AS GreenMasteryCount
FROM         tblProgress INNER JOIN
                       tblEnrollment ON tblProgress.StudentID =
tblEnrollment.StudentID


GO

---------------------------------------

Any ideas?

Thanks,
--Gary

 
 
 

Newbie Q: Multiple Recordsets Returned as One in SP

Post by Bob Pfeiff [MS » Wed, 22 May 2002 04:41:52


Perhaps a better way to do this than produce three resultsets would be to
have three output parameters in the stored proc and use an ADO Command
object to execute it and read the output values from the Command object's
Parameters collection.  The stored proc would look like:

CREATE PROCEDURE userSP_sysTeacherAttainMasteryCount






AS

SELECT     COUNT(*) AS RedMasteryCount
FROM         tblProgress INNER JOIN
                       tblEnrollment ON tblProgress.StudentID =
tblEnrollment.StudentID

SELECT     COUNT(*) AS YellowMasteryCount
FROM         tblProgress INNER JOIN
                       tblEnrollment ON tblProgress.StudentID =
tblEnrollment.StudentID


SELECT     COUNT(*) AS GreenMasteryCount
FROM         tblProgress INNER JOIN
                       tblEnrollment ON tblProgress.StudentID =
tblEnrollment.StudentID


GO

Otherwise, you need to use the NextRecordset method of the ADO Recordset
object to get all three values, you should probably include SET NOCOUNT ON
at the top of the stored proc (after AS) to prevent the rows affected
messages from showing up as result sets themselves.

--
Bob
Microsoft Consulting Services
------
This posting is provided AS IS with no warranties, and confers no rights.

 
 
 

Newbie Q: Multiple Recordsets Returned as One in SP

Post by Anith Se » Wed, 22 May 2002 04:45:07


Use a GROUP BY MasteryLevel, this helps you get a single Resultset
with MasteryLevel 1,2,3 etc with corresponding COUNTs

CREATE PROCEDURE userSP_sysTeacherAttainMasteryCount



AS
SELECT  tblProgress.MasteryLevel,
 COUNT(*) AS MasteryCount
FROM
 tblProgress INNER JOIN
        tblEnrollment ON tblProgress.StudentID = tblEnrollment.StudentID
WHERE

AND

AND

GROUP BY
 tblProgress.MasteryLevel

- Anith


> Forgive my ignorance.  I want to pass a few variables to a stored
procedure,
> ask the stored procedure to run three different SELECT statements (with a
> single value each), then return the results of all three as one resultset
to
> be used with an ASP page.

> My attempt is shown below.  While this works in Query Analyzer, it does so
> by returning three separate recordsets:

> -----------------------------------------

> CREATE PROCEDURE userSP_sysTeacherAttainMasteryCount



> AS

> SELECT     COUNT(*) AS RedMasteryCount
> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation=

> SELECT     COUNT(*) AS YellowMasteryCount
> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation

> SELECT     COUNT(*) AS GreenMasteryCount
> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation

- Show quoted text -


> GO

> ---------------------------------------

> Any ideas?

> Thanks,
> --Gary

 
 
 

Newbie Q: Multiple Recordsets Returned as One in SP

Post by Peter Maho » Thu, 23 May 2002 00:32:42


Gary,

    Why not simply use a UNION query to concatenate the recordsets into a
single recordset like so...
SELECT     COUNT(*) AS RedMasteryCount

> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation=
    union

> SELECT     COUNT(*) AS YellowMasteryCount
> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation
    union

> SELECT     COUNT(*) AS GreenMasteryCount
> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation



> Forgive my ignorance.  I want to pass a few variables to a stored
procedure,
> ask the stored procedure to run three different SELECT statements (with a
> single value each), then return the results of all three as one resultset
to
> be used with an ASP page.

> My attempt is shown below.  While this works in Query Analyzer, it does so
> by returning three separate recordsets:

> -----------------------------------------

> CREATE PROCEDURE userSP_sysTeacherAttainMasteryCount



> AS

> SELECT     COUNT(*) AS RedMasteryCount
> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation=

> SELECT     COUNT(*) AS YellowMasteryCount
> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation

> SELECT     COUNT(*) AS GreenMasteryCount
> FROM         tblProgress INNER JOIN
>                        tblEnrollment ON tblProgress.StudentID =
> tblEnrollment.StudentID

=


(tblEnrollment.Designation

- Show quoted text -


> GO

> ---------------------------------------

> Any ideas?

> Thanks,
> --Gary

 
 
 

1. Multiple SPs OR one SP w/ Multiple Recordsets (Part 2)

Hi,

There are some pretty knowledgable people here and I'd like to thank them
for sharing their wisdom and experience with us.

This was a question I brought up a while ago and Aaron and some other guys
were nice enough to answer my question. I'd like to go a little deeper this
time if I may.

What I learned from my previous question was that one big stored procedure
with multiple recordsets was more efficient than many smaller stored
procedures. I now have a stored procedure that produces 7 recordsets.
Performance-wise I'm pretty happy with though it's not really used in a very
demanding environment.

My question is: why is it that one SP is more efficient than many SPs? What
really eats up more system resources in terms of CPU cycles or memory or
both? Is it the connection to the DB? I'd really appreciate your
elaboration. Thanks.

Sam

2. Pick Programmer/Analyst Position in So. CA

3. INFORMIX DBA/Career Position/Tenn

4. Return only one recordset from SP

5. Free MSDE 2000 vs. paid SQL 2000 Server

6. Return multiple recordsets from SP

7. Oracle .DMP file to SQL Server 2000

8. problem with sp returning multiple recordsets

9. Return multiple recordsets from SP

10. Cursor returning multiple recordset intead of one...

11. Returning a recordset from a Sp to another SP