MySQL Error: 1337

You need to re-order your declarations.

Cursor declarations must appear before handler declarations 
and after variable and condition declarations.

http://dev.mysql.com/doc/refman/5.1/en/cursors.html


It's complaining about:

Variable or condition declaration after cursor or handler declaration

So I would be looking at that location, where you do indeed declare variables after the cursor and handler:

   DECLARE employees CURSOR FOR SELECT empid FROM employees;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;
>> DECLARE emp VARCHAR(20);

If you modify your declarations thus, it should be okay:

DECLARE fromDt DATETIME;
DECLARE toDt DATETIME;
DECLARE done INT DEFAULT FALSE;
DECLARE emp VARCHAR(20);

DECLARE employees CURSOR FOR SELECT empid FROM employees;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;

The order has to be, as per here:

Cursor declarations must appear before handler declarations and after variable and condition declarations.

Tags:

Mysql