I've started to examine Procedures in MySQL, but all my efforts don't work. Here is my Procedure Creation:
DELIMITER //
CREATE PROCEDURE test(IN a INT)
BEGIN
SELECT *
FROM `table`
WHERE `id` = a;
END
MySQL returns O.K., no errors.
DELIMITER ;
MySQL returns O.K., no errors.
But CALL-Statement does not work:
CALL test(8);
Returns an error: #1312 - PROCEDURE dbxyz.test can't return a result set in the given context
Now, I don't know what I've made wrong: a mistake in Procedure-Cration or an error in CALL-Statement.
--> ##########################
As of 6th Feb. 2014: Today, I've tried to find out, why my Stored Procedure does not work in Query Window of phpMyAdmin. A 'SELECT *' does not work within a Stored Procedure, but a SELECT column by column does work. Here is what I have found out: Use IN and OUT as well as INTO. Example:
DELIMITER //
CREATE PROCEDURE test(IN a INT, OUT b VARCHAR(12), OUT c INT)
BEGIN
SELECT b, c
FROM `table`
WHERE `id` = a
INTO b, c;
END
Now, this stored Procedure is able to run in phpMyAdmin:
call test(5, @result1, @result2);
SELECT @result1, @result2
If you just need ONE result instad of two or more, you could also use SET - Statement. Here we are:
DELIMITER //
CREATE PROCEDURE test(IN a INT, OUT b INT)
BEGIN
SET b = ( SELECT b
FROM table
WHERE id = a );
END
see also comment above:
Today, I've tried to find out, why my Stored Procedure does not work. A 'SELECT *' does not work within a Stored Procedure, but a SELECT column by column does work. Here is what I have found out: DELIMITER // CREATE PROCEDURE test(IN a INT, OUT b VARCHAR(12), OUT c INT) BEGIN SELECT b, c FROM table WHERE id = a INTO b, c; END Now I can run my Procedure: call test(5, @result1, @result2); SELECT @result1, @result2 – Peter Feb 6 '14 at 11:09