Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored Procedure return multiple result sets

I need a SP to return multiple sets of results. The second set of results would be based on a column of the first set of results.

So:

declare @myTable1 table(field0 int,field1 varchar(255))
insert into @myTable1 select top 1 field0, field1 from table1

declare @myTable2 table(field0 int,field3 varchar(255))
insert into @myTable2 
select field0, field3 from table2 
where @myTable1.field0 = @myTable2.field0

How do return @myTable1 and @myTable2 with my SP? Is this syntax even right at all?

My apologies, I'm still a newbie at SQL...

EDIT:

So, I'm getting an error on the last line of the code below that says: "Must declare the scalar variable "@myTable1""

declare @myTable1 table(field0 int,field1 dateTime)
insert into @myTable1 
select top 1 field0, field1 
from someTable1 m
where m.field4 > 6/29/2009

select * from @myTable1
select *
from someTable2 m2
where m2.field0 = @myTable1.field0

If I highlight and run the code up until the second select * it works fine... when I highlight the rest it acts like the first variable doesn't exist...

EDIT2: Figured that problem out. Thanks guys.

declare @myTable1 table(field0 int,field1 dateTime)
insert into @myTable1 
select top 1 field0, field1 
from someTable1 m
where m.field4 > 6/29/2009

select * from @myTable1
select *
from someTable2 m2
where m2.field0 = (select field0 from @myTable1)
like image 685
EJC Avatar asked Oct 06 '10 19:10

EJC


People also ask

What can be used to return multiple result sets?

In order to get multiple result sets working we need to drop to the ObjectContext API by using the IObjectContextAdapter interface. Once we have an ObjectContext then we can use the Translate method to translate the results of our stored procedure into entities that can be tracked and used in EF as normal.

Can a stored procedure return a result set?

In addition to returning output parameters, a stored procedure can return a result set (that is, a result table associated with a cursor opened in the stored procedure) to the application that issues the CALL statement.

Can stored procedure return multiple result sets Oracle?

You can use Oracle stored procedures that return a single result set in a report. If your stored procedure returns multiple result sets, you must use embedded SQL commands to access it. The following procedure assumes that your Oracle stored procedure returns only a single result set.

How can I get multiple output from stored procedure in SQL?

In order to fetch the multiple returned values from the Stored Procedure, you need to make use of a variable with data type and size same as the Output parameter and pass it as Output parameter using OUTPUT keyword. You can also make use of the Split function to split the comma separated (delimited) values into rows.


2 Answers

You pretty much just select two result sets

SELECT * FROM @myTable1
SELECT * FROM @myTable2

However, some tools will hide some results (e.g. pgAdmin will only show the last) and some tools have some sort of requirement to get to the next result set (e.g. .NET's IDataReader's will not allow you to Read() from the second resultset until you call NextResult()).

Edit:

An alternative in this case, since the types of the two results match, is to combine them into a single resultset:

SELECT field0, field1 from @myTable1
UNION
SELECT field0, field3 from @myTable2

You can also choose between UNION ALL or UNION DISTINCT (the default) where the latter will only send rows that aren't repeats.

like image 124
Jon Hanna Avatar answered Oct 12 '22 13:10

Jon Hanna


At the end of the Stored Proc, put:

SELECT * FROM @myTable1
SELECT * FROM @myTable2

This will return 2 result sets.

like image 44
JNK Avatar answered Oct 12 '22 12:10

JNK