简体   繁体   中英

MYSQL Error #2014: in stored procedure when resultset is empty

I am trying to make a stored procedure in MYSQL(v5.7.21) with phpmyadmin(v4.7.9) that will sometimes return an empty result set.

CREATE DEFINER=`my_database`@`%` PROCEDURE `emptytest`(IN `_id` INT(11))
NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
    select * from my_table where id=_id

and next call it with:

call emptytest(1);

this works fine when the id exists in the table:

id  name
--------
1   bob

but throws an error when there are no rows to return:

call emptytest(11);
#2014 - Commands out of sync; you can't run this command now

However i would expect it to return the same as running the SQL statement:

select * from my_table where id=11

Which is an empty list:

id  name
--------
  • I've been looking on StackOverflow for similar questions but most of them address issues with multiple queries which is not my case.

  • As far as I know MySQL documentation states that procedures should be able to return tables.

  • For the example I am showing the default options used by phpmyadmin, but i tried to tweak them to no avail.

What am I missing?

Nothing wrong with the procedure.

It is phpMyAdmin which does not handle stored procedures when executed using CALL in SQL tab.

You can run the procedure by clicking the icon in front of the procedure name in the left navigation tree.

Stored procedures tend to be 'fussier' about errors than simple statements (see below), so you need to put a handler in for that error.

DECLARE EXIT HANDLER FOR 2014 BEGIN select 'empty set' as `Error`; END;

The principle here is that you can't really handle these sort of errors in a simple query, so, from necessity, they are ignored; you can handle them in an SP, so, well, handle them...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM