How do I call a procedure in MySQL?

I started learning 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 OK, no errors.

 DELIMITER ;

      

MySQL returns OK, no errors.

But CALL-Statement doesn't work:

CALL test(8);

      

Returns error: # 1312 - PROCEDURE dbxyz.test cannot return a result set in the given context

Now I don't know what I did wrong: an error in the Procedure-Cration or an error in the CALL-Statement.

-> ##########################

From Feb 6, 2014: Today I tried to figure out why my stored procedure is not working in the phpMyAdmin requests window. "SELECT *" doesn't work in a stored procedure, but column-by-column SELECT does. Here's what I figured out: use IN and OUT and 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 can run in phpMyAdmin:

 call test(5, @result1, @result2);
 SELECT @result1, @result2

      

If you only want one of two or more results, you can 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

      

0


source to share


1 answer


see also comment above:



Today I tried to figure out why my stored procedure is not working. "SELECT *" does not work in a stored procedure, but SELECT column by column does. Here's what I found out: DELIMITER // CREATE PROCEDURE test (IN 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 6 February 14 at 11:09

0


source







All Articles