Bug #51228 LOOP / LEAVE / END LOOP error when LABEL is not being specified
Submitted: 17 Feb 2010 0:22 Modified: 17 Feb 2010 4:35
Reporter: Ricardo M Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Stored Routines Severity:S3 (Non-critical)
Version: 5.1.42-community-log, 5.1.45-bzr OS:Any
Assigned to: CPU Architecture:Any
Tags: loop

[17 Feb 2010 0:22] Ricardo M
Description:
From reference manual:

12.8.6.3. LOOP Statement
[begin_label:] LOOP
    statement_list
END LOOP [end_label]

12.8.6.4. LEAVE Statement
LEAVE label

The begin_label should be optional but source command complains when is not specified with ERROR 1064 (42000)

How to repeat:
Here is how to reproduce it:
create test_loop.sql script
delimiter !!

DROP PROCEDURE IF EXISTS test_loop;
CREATE PROCEDURE test_loop(IN p_param      bigint  unsigned)
BEGIN
    DECLARE var int  DEFAULT 0;
    
    LOOP
       IF var = 0 THEN
          SET var := 1;
       ELSE
          LEAVE;
       END IF;
    END LOOP;

END;
!!       
delimiter ;

mysql> source test_loop.sql;
Query OK, 0 rows affected (0.00 sec)

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';
       END IF;
    END LOOP;

END' at line 9

Now I will add as LABEL LOOPING (here it works as expected):
create test_loop.sql script
delimiter !!

DROP PROCEDURE IF EXISTS test_loop;

CREATE PROCEDURE test_loop(IN p_param     bigint  unsigned)
BEGIN
    DECLARE var int  DEFAULT 0;
    
    LOOPING: LOOP
       IF var = 0 THEN
          SET var := 1;
       ELSE
          LEAVE LOOPING;
       END IF;
    END LOOP LOOPING;

END;
!!       
delimiter ;

mysql> source test_loop.sql;
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Also I got error if as LABEL I use the word LOOP:
create test_loop.sql script
delimiter !!

DROP PROCEDURE IF EXISTS test_loop;

CREATE PROCEDURE test_loop(IN p_usageId               bigint  unsigned)
BEGIN
    DECLARE var int  DEFAULT 0;
    
    LOOP: LOOP
       IF var = 0 THEN
          SET var := 1;
       ELSE
          LEAVE LOOP;
       END IF;
    END LOOP LOOP;

END;
!!       
delimiter ;

mysql> source test_loop.sql;
Query OK, 0 rows affected, 1 warning (0.00 sec)

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ': LOOP
       IF var = 0 THEN
          SET var := 1;
       ELSE
          LEAV' at line 5

Suggested fix:
As workaround define a LABEL other than LOOP reserver word.
[17 Feb 2010 4:35] Valeriy Kravchuk
While I can repeat this on recent 5.1.45, our manual for LEAVE (http://dev.mysql.com/doc/refman/5.1/en/leave-statement.html) says that label is NOT optional for LEAVE, you must provide it. Hence the error message you get, and error position. 

LOOP is a reserved word, so can not be used as label. 

Looks like formally there is no bug here.