views:

619

answers:

3

I'm having a rather strange problem with MySQL. Trying to create a procedure to update some fields in the database (the code is below).

The problem is with the line that is currently commented. It seems that if no SELECT statements get executed during the procedure MySQL query browser will return an error code of "-1, error executing SQL query".

I tried the same thing in HeidiSQL and the error was "cannot return result set". So I suppose the question is do I always have to select something in the procedure, or is there some other thing I missed.

The query works fine when the comment is removed.

DELIMITER /
DROP PROCEDURE IF EXISTS updateFavourites /
CREATE PROCEDURE updateFavourites(quota INT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE artist_id,releases INT;
DECLARE c_artist Cursor FOR
    SELECT Artist.id_number,COUNT(Artist.id_number) FROM Artist
    JOIN CD ON CD.is_fronted_by = Artist.id_number
GROUP BY Artist.id_number;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'
    SET done=1;

IF quota > 0 THEN
    OPEN c_artist;
    REPEAT
    FETCH c_artist INTO artist_id,releases;
     IF NOT done THEN
  IF releases >= quota THEN
   UPDATE CD SET CD.rating='favourite' WHERE CD.is_fronted_by = artist_id;
  END IF;
 END IF;
UNTIL done END REPEAT;
CLOSE c_artist;
-- SELECT 'Great success';
ELSE
    SELECT CONCAT('\'quota\' must be greater than 0.',' Got (',quota,')'); 
END IF;

END /
DELIMITER ;

Here's the sql to create the tables and some data:

DROP TABLE IF EXISTS CD;
DROP TABLE IF EXISTS Artist;

CREATE TABLE Artist (
id_number INT UNSIGNED AUTO_INCREMENT  PRIMARY KEY,
name VARCHAR(50),
);

CREATE TABLE CD (
catalog_no   INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
is_fronted_by    INT UNSIGNED,
rating    ENUM ('favourite','top draw','good','so-so','poor','rubbish'),
CONSTRAINT fk_CD_Artist  FOREIGN KEY (is_fronted_by) REFERENCES Artist(id_number) ON UPDATE     CASCADE
);

INSERT INTO Artist VALUES(11,'Artist 1');
INSERT INTO Artist VALUES(10,'Artist 2');
INSERT INTO CD VALUES (7,11, 'top draw');
INSERT INTO CD VALUES (650,11,'good');
INSERT INTO CD VALUES (651,11,'good');
INSERT INTO CD VALUES (11,10,'favourite');
A: 

Try putting BEGIN and END blocks around the multiple statements in the IF block as such:

IF quota > 0 THEN
    BEGIN
        OPEN c_artist;
        REPEAT
        FETCH c_artist INTO artist_id,releases;
        IF NOT done THEN
            IF releases >= quota THEN
                UPDATE CD SET CD.rating='favourite' WHERE CD.is_fronted_by = artist_id;
            END IF;
        END IF;
        UNTIL done END REPEAT;
        CLOSE c_artist;
    END;
ELSE
    SELECT CONCAT('\'quota\' must be greater than 0.',' Got (',quota,')'); 
END IF;
Neal Swearer
That compiles but still comes up with the same error -1.
Matti
+1  A: 

Googling around, there are several reports of the same error, but little information to solve the problem. There's even a bug logged at mysql.com but it appears to have been abandoned without being resolved.

There's another StackOverflow question on the same error, but it's also unresolved.

All it means is that there is no result set from the query. Looking at the source code, it appears that sometimes an error status of MYX_SQL_ERROR is set when the query has no result set. Perhaps this is not an appropriate consequence?

I notice that when I use the mysql command-line client, it yields no error for calling a proc that returns no result set.


update: I tried to revive that MySQL bug report, and provide a good test case for them. They changed the bug from "no feedback" to "verified" -- so at least they acknowledge it's a bug in Query Browser:

[11 Dec 9:18] Sveta Smirnova

Bill,

thank you for the feedback. Verified as described.

Although most likely this only be fixed when MySQL Query Browser functionality is part of MySQL workbench.

I guess the workaround is to ignore the -1 error, or to test your stored procedures in the command-line mysql client, where the error does not occur.

The comment supposes the issue will disappear as the Query Browser functionality becomes part of MySQL Workbench. This is supposed to happen in MySQL Workbench 5.2. I'll download this beta and give it a try.

MySQL Workbench 5.2 is in Beta, but I would assume MySQL engineering can't predict when the Beta will become GA. Those kinds of predictions are hard enough under standard conditions, but there's a lot of extra uncertainty of MySQL's fate due to the unresolved Oracle acquisition.


update: Okay, I have tried MySQL Workbench 5.2.10 beta. I executed a stored procedure like this:

CREATE PROCEDURE FooProc(doquery SMALLINT)
BEGIN
  IF doquery THEN
    SELECT * FROM Foo;
  END IF;
END

When I CALL FooProc(0) the response is no result set, and the status is simply "OK".

When I CALL FooProc(1) the response is the result of SELECT * FROM Foo as expected.

However, there's another bug related to calling procedures. Procedures may have multiple result sets, so it's hard to know when to close the statement when you execute a CALL query. The consequence is that MySQL Workbench 5.2 doesn't close the statement, and if you try to do another query (either CALL or SELECT) it gives you an error:

Commands out of sync; you can't run this command now.

MySQL doesn't support multiple concurrent open queries. So the last one must be closed before you can start a new one. But it isn't closing the CALL query. This bug is also logged at the MySQL site.


The bug about commands out of sync has been resolved. They say it's fixed in MySQL Workbench 5.2.11.

Bill Karwin
Hi, yeah I found the same bug report on mysql.com, and a few unresolved threads dispersed here and there on the issue. So would you say that this is a problem with the query browser client?
Matti
Yes; see my update above.
Bill Karwin
reg. "Commands out of sync": beta is beta
noonex
+2  A: 

Query Browser is not for running scripts, just single query. I tried your code by moving cursor into each query (except DELIMITER) and pressing Ctrl+Enter. It created that stored procedure without problem. (just refresh schema on the left).

If you wish creating procedure, use menu "Script"->"Create stored procedure/function".

But better forget about QueryBrowser it is not supported at all (and actunally not useful). If you have decent hardware and plenty resources, try Workbench 5.2 otherwise use SQLyog

noonex
Ok, tried workbench 5.2 beta. That seems to be fine, so then the problem is mysql query browser and jHeidi.Thanks for this.
Matti