Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Stored Procedures not working with SELECT (basic question)

I am using a platform (perfectforms) that requires me to use stored procedures for most of my queries, and having never used stored procedures, I can't figure out what I'm doing wrong. The following statement executes without error:

DELIMITER //
DROP PROCEDURE IF EXISTS test_db.test_proc//
CREATE PROCEDURE test_db.test_proc() SELECT 'foo'; //
DELIMITER ;

But when I try to call it using:

CALL test_proc();

I get the following error:

#1312 - PROCEDURE test_db.test_proc can't return a result set in the given context

I am executing these statements from within phpmyadmin 3.2.4, PHP Version 5.2.12 and the mysql server version is 5.0.89-community.

When I write a stored procedure that returns a parameter, and then select it, things work fine (e.g.):

DELIMITER //
DROP PROCEDURE IF EXISTS test_db.get_sum//
CREATE PROCEDURE test_db.get_sum(out total int)
BEGIN
SELECT SUM(field1) INTO total FROM test_db.test_table;
END //
DELIMITER ;

works fine, and when I call it:

CALL get_sum(@t); SELECT @t;

I get the sum no problem.

Ultimately, what I need to do is have a fancy SELECT statement wrapped up in a stored procedure, so I can call it, and return multiple rows of multiple fields. For now I'm just trying to get any select working.

Any help is greatly appreciated.

like image 715
TMG Avatar asked Mar 02 '10 01:03

TMG


People also ask

Can we use stored procedure in SELECT query?

We can not directly use stored procedures in a SELECT statement.

Why do we don't we call stored procedures with a SELECT statement?

Biggest reasons most likely is that procedures can return any number of result sets and change data. It can have no results, or it can be 100 different results sets with 0 to n rows. It can also depend on your input parameters.

How do I SELECT a stored procedure in MySQL?

In MySQL, it is not possible to use select from procedure in FROM clause. You can use CALL command and after that the SELECT statement can be executed. Here is the query to display records from the table using select statement after calling stored procedure.

Which function Cannot be used in stored procedure in MySQL?

LOAD DATA and LOAD XML . SQL prepared statements ( PREPARE , EXECUTE , DEALLOCATE PREPARE ) can be used in stored procedures, but not stored functions or triggers. Thus, stored functions and triggers cannot use dynamic SQL (where you construct statements as strings and then execute them).


2 Answers

Figured it out. This is not a bug with PHP (though it used to be) - it's a bug in some versions of phpmyadmin. The same bug intermittently reappears and is then fixed in various subversions (see above):

#1312 - PROCEDURE [name] can't return a result set in the given context

This behavior appears limited to SELECT statements within stored procedures inside phpmyadmin.

Using a client like MySQL Workbench works around the problem (or you could upgrade phpmyadmin, but that's a pain if you're on a shared server like I am).

Anyway, thanks to everyone for your help.

like image 182
TMG Avatar answered Nov 14 '22 21:11

TMG


Check your php version to see if this is a reported bug (see here).

See this post: Can't return a result set in the given context

like image 23
dugas Avatar answered Nov 14 '22 23:11

dugas