<p>MySQL raises ER_SP_GOTO_IN_HNDLR when a GOTO statement is placed inside a DECLARE ... HANDLER block in a stored procedure or function, which the engine forbids.</p>
<p>MySQL Error 1358 ER_SP_GOTO_IN_HNDLR appears when a GOTO statement is used inside a stored procedure handler. Replace GOTO with LEAVE, RESIGNAL, or restructure the logic to eliminate the forbidden jump.</p>
GOTO is not allowed in a stored procedure handler
The server throws this error when the parser sees a GOTO inside a DECLARE ... HANDLER block during stored procedure compilation. MySQL forbids altering control flow from within an error or condition handler using GOTO, so compilation stops with error 1358.
The error surfaces at creation time of a stored procedure, stored function, or trigger. It never reaches runtime because the program unit fails to compile once the forbidden GOTO is detected in a handler.
Unfixed, the routine cannot be created or altered, blocking deployments and breaking dependent application logic. Production releases stall until developers remove the illegal statement.
The sole cause is using a GOTO inside a DECLARE EXIT, CONTINUE, or UNDO HANDLER. MySQL’s parser disallows jumps from handler scope to another label because it could corrupt the condition stack.
Rewrite the handler without GOTO. Use LEAVE to exit labeled blocks, SIGNAL or RESIGNAL to propagate errors, or move the cleanup logic outside the handler so normal flow-control statements suffice.
Design stored programs with structured flow: BEGIN/END blocks, IF/ELSE, LOOP/LEAVE. Reserve handlers for error capture, not general branching. Validate routines in a staging server or Galaxy’s SQL editor linting before committing.
Compilation may also fail with ER_SP_LABEL_REDEFINE or ER_SP_UNDECLARED_VAR when labels or variables are misused. These issues share the root cause of invalid flow control and are fixed by restructuring code.
Developers often add GOTO inside an EXIT handler to jump to a cleanup label, triggering ER_SP_GOTO_IN_HNDLR.
A CONTINUE handler that tries to resume execution at a distant label with GOTO will fail for the same reason.
Porting T-SQL or PL/SQL routines that freely use GOTO in handlers leads to this error because MySQL’s flow-control rules differ.
Raised when a label is defined more than once in the same scope.
Occurs when you reference a variable that was never declared in the routine.
Signals that a cursor OPEN statement is executed while the cursor is already open.
No. MySQL forbids GOTO in any handler scope. Use LEAVE, ITERATE, SIGNAL, or restructure your code.
No. The restriction exists in all supported MySQL releases, including 5.7, 8.0, and MariaDB derivatives.
Yes, as long as the target label encloses the handler. LEAVE cannot jump to outer scopes beyond that label.
Galaxy’s real-time linting flags disallowed statements like GOTO inside handlers before you run CREATE PROCEDURE, preventing the compilation failure.