Am trying to fetch out a field through stored procedure and I used following query. I aimed at fetching out multiple rows, but it executes the result successfully only when a single row exists. Or else it returns an error as I mentioned below.
DELIMITER ;;
DROP PROCEDURE IF EXISTS Sample1;;
CREATE PROCEDURE Sample1(IN lft1 INT, IN rgt1 INT, OUT emp1 VARCHAR(20))
BEGIN
SELECT p.emp INTO emp1
FROM personnel p
WHERE p.lft > lft1
AND p.rgt < rgt1
LIMIT 10;
END;;
CALL Sample1(1, 10, @emp);;
SELECT @emp;
MySQL said: Documentation
#1172 - Result consisted of more than one row
Sample1
- procedure nameemp
- selected field from table personnellft
- use to check the condition, it is also one of the field of table personnelpersonnel
- table name
You can't return multiple values from stored procedure in the way you are doing it now. You can, however, specify your parameters to be OUTPUT so you can access them.
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.
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.
The error is not in your procedure. The error is in your query - it returns more then one row, but you cannot set multiple result into scalar value 'emp1'.
You should limit your query so that it returns one row.
How to retreive multiple rows from stored procedure in mysql?
Just had the same question. After a little research I found a solution in the official MySQL documentation:
It requires MySQL 5.5.3 or higher.
In contrast to the inital stored procedure from @Bala.C it doesn't use an out
parameter.
CREATE PROCEDURE get_data ()
BEGIN
SELECT Code, Name, Population, Continent
FROM Country
WHERE Continent = 'Oceania'
AND Population < 10000;
SELECT Code, Name, Population, Continent
FROM Country
WHERE Continent = 'Europe'
AND Population < 10000;
SELECT Code, Name, Population, Continent
FROM Country
WHERE Continent = 'North America'
AND Population < 10000;
END;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With