Common SQL Errors

MySQL Error 1358: ER_SP_GOTO_IN_HNDLR - How to Fix and Prevent

Galaxy Team
August 6, 2025

<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>

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1358 ER_SP_GOTO_IN_HNDLR?

<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>

Error Highlights

Typical Error Message

GOTO is not allowed in a stored procedure handler

Error Type

Syntax Error

Language

MySQL

Symbol

ER_SP_GOTO_IN_HNDLR

Error Code

1358

SQL State

HY000

Explanation

Table of Contents

What does MySQL error 1358 ER_SP_GOTO_IN_HNDLR mean?

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.

When does the error appear?

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.

Why is it important to fix quickly?

Unfixed, the routine cannot be created or altered, blocking deployments and breaking dependent application logic. Production releases stall until developers remove the illegal statement.

What causes this error?

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.

How do you fix MySQL error 1358?

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.

Best practices to avoid ER_SP_GOTO_IN_HNDLR

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.

Related errors and solutions

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.

Common Causes

GOTO inside EXIT handler

Developers often add GOTO inside an EXIT handler to jump to a cleanup label, triggering ER_SP_GOTO_IN_HNDLR.

GOTO inside CONTINUE handler

A CONTINUE handler that tries to resume execution at a distant label with GOTO will fail for the same reason.

Copying code from other RDBMS

Porting T-SQL or PL/SQL routines that freely use GOTO in handlers leads to this error because MySQL’s flow-control rules differ.

Related Errors

ER_SP_LABEL_REDEFINE (Error 1331)

Raised when a label is defined more than once in the same scope.

ER_SP_UNDECLARED_VAR (Error 1314)

Occurs when you reference a variable that was never declared in the routine.

ER_SP_CURSOR_ALREADY_OPEN (Error 1336)

Signals that a cursor OPEN statement is executed while the cursor is already open.

FAQs

Can I ever use GOTO inside a MySQL handler?

No. MySQL forbids GOTO in any handler scope. Use LEAVE, ITERATE, SIGNAL, or restructure your code.

Does the error depend on MySQL version?

No. The restriction exists in all supported MySQL releases, including 5.7, 8.0, and MariaDB derivatives.

Is LEAVE always a safe replacement?

Yes, as long as the target label encloses the handler. LEAVE cannot jump to outer scopes beyond that label.

How does Galaxy help avoid this error?

Galaxy’s real-time linting flags disallowed statements like GOTO inside handlers before you run CREATE PROCEDURE, preventing the compilation failure.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo