1329
MariaDBerrorstored-procedureshigh confidence

Variable or condition must be declared before cursors or handlers

Production Risk

Low — compile-time error; routine not created.

What this means

A DECLARE for a variable or condition appears after a DECLARE CURSOR or DECLARE HANDLER in the same block, violating MySQL declaration ordering rules.

Why it happens
  1. 1Variable DECLARE placed after CURSOR DECLARE in the same BEGIN...END block
How to reproduce
trigger — this will error
trigger — this will error
CREATE PROCEDURE p() BEGIN DECLARE cur CURSOR FOR SELECT 1; DECLARE v INT DEFAULT 0; END;

expected output

ERROR 1329 (42000): Variable or condition must be declared before cursors or handlers

Fix

Reorder declarations: variables, then conditions, then cursors, then handlers

Reorder declarations: variables, then conditions, then cursors, then handlers
CREATE PROCEDURE p() BEGIN DECLARE v INT DEFAULT 0; DECLARE cur CURSOR FOR SELECT 1; END;

Why this works

MySQL enforces strict declaration ordering in each block.

Sources
Official documentation ↗

MySQL 8.0 — 1329 ER_SP_VARCOND_AFTER_CURSHNDLR

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors