Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Multiple Result Sets from MYSQL Stored Procedure

How do you return multiple result sets from a MYSQL Stored Procedure?

This is my test stored proc:

DELIMITER $$

CREATE DEFINER=`hlamAdmin`@`%` PROCEDURE `test`()
BEGIN
   SELECT *
   FROM hlam.member;

   SELECT * 
   FROM hlam.security;

END

Now when I call this:

Call test()

I only receive one resultset. How do I receive both? I am used to MSSQL I apologize if this is an easy question.

like image 599
jtmg.io Avatar asked Sep 10 '11 23:09

jtmg.io


People also ask

Can a stored procedure return multiple result sets?

Most stored procedures return multiple result sets. Such a stored procedure usually includes one or more select statements. The consumer needs to consider this inclusion to handle all the result sets.

How can I return multiple values from a stored procedure in MySQL?

MySQL stored function returns only one value. To develop stored programs that return multiple values, you need to use stored procedures with INOUT or OUT parameters. If you are not familiar with INOUT or OUT parameters, check it out the stored procedure's parameters tutorial for the detailed information.

How can I return multiple values from a stored procedure?

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.


1 Answers

You could try to either JOIN them (Linking them together) or use UNION (Combine two selects in one);

http://dev.mysql.com/doc/refman/5.0/en/join.html

select a.col1, b.col1
from table1 a
inner join table2 b on a.id = b.id;

http://dev.mysql.com/doc/refman/5.0/en/union.html

select name as col1, surname as col2 from table1
union
select location as col1, desc as col2 from table2;

John

like image 71
John Avatar answered Nov 12 '22 05:11

John