1330
MariaDBerrorstored-procedureshigh confidence
Cursor must be declared before handlers
Production Risk
Low — compile-time error; routine not created.
What this means
A DECLARE CURSOR appears after a DECLARE HANDLER in the same block, violating MySQL declaration ordering rules.
Why it happens
- 1Handler DECLARE placed before cursor DECLARE in the same BEGIN...END block
How to reproduce
trigger — this will error
trigger — this will error
CREATE PROCEDURE p() BEGIN DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; DECLARE cur CURSOR FOR SELECT 1; END;
expected output
ERROR 1330 (42000): Cursor must be declared before handlers
Fix
Place cursor declarations before handler declarations
Place cursor declarations before handler declarations
CREATE PROCEDURE p() BEGIN DECLARE cur CURSOR FOR SELECT 1; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; END;
Why this works
Follows the required ordering: variables -> conditions -> cursors -> handlers.
Sources
Official documentation ↗
MySQL 8.0 — 1330 ER_SP_CURSOR_AFTER_HANDLER
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev