Common SQL Errors

MySQL Error 1420: ER_EXEC_STMT_WITH_OPEN_CURSOR - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>MySQL raises error 1420 when you try to execute a prepared statement that still has an open cursor from a previous FETCH.</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 1420: ER_EXEC_STMT_WITH_OPEN_CURSOR?

<p>MySQL Error 1420: ER_EXEC_STMT_WITH_OPEN_CURSOR occurs when you call EXECUTE on a prepared statement while its cursor remains open after a FETCH. Close the cursor with CLOSE or reset the statement before re-executing.</p>

Error Highlights

Typical Error Message

You can't execute a prepared statement which has an open

Error Type

Cursor Error

Language

MySQL

Symbol

ER_EXEC_STMT_WITH_OPEN_CURSOR

Error Code

1420

SQL State

HY000

Explanation

Table of Contents

What does MySQL Error 1420 mean?

Error 1420 (SQLSTATE HY000) appears with the message: You can't execute a prepared statement which has an open cursor associated with it. Reset the statement to re-execute it.

The server blocks EXECUTE because the current prepared statement still owns an active cursor following a FETCH. MySQL requires that cursor life-cycle commands finish before re-execution.

When does the error usually surface?

The error surfaces in stored procedures, server-side prepared statements, and API code that loops through result sets using cursors and then tries to execute the same statement again without closing or resetting it.

Why is resolving it important?

Leaving cursors open wastes server resources and can stall transaction logic. Properly closing the cursor restores statement reusability and prevents application downtime.

What Causes This Error?

The primary trigger is calling EXECUTE on a prepared statement that still has a cursor open after a FETCH operation.

Additional causes include missing CLOSE commands in stored procedures, error paths that exit loops prematurely, and client libraries that implicitly reuse prepared statements without cleaning up.

How to Fix MySQL Error 1420

Always CLOSE the cursor before calling EXECUTE again. If you need to rerun the statement many times, wrap the sequence OPEN - FETCH - CLOSE inside a loop.

If using a client API, call stmt.reset() or conn.unprepare() equivalents before re-executing.

Common Scenarios and Solutions

Stored procedures that iterate over large result sets must explicitly CLOSE the cursor inside the loop exit branch.

Micro-services using connection pools should ensure each borrowed connection resets statements before return.

Best Practices to Avoid This Error

Adopt try...finally blocks in SQL routines: OPEN, FETCH, CLOSE in the finally section.

Monitor performance_schema.prepare_stmt_instances to catch long-lived cursors early.

Related Errors and Solutions

Error 1329: No data - zero rows fetched, but cursor left open if not closed.

Error 1243: Unknown prepared statement handler - triggered when you CLOSE a cursor that was never opened.

Common Causes

Forgotten CLOSE Statement

The cursor is opened and fetched but never closed before the next EXECUTE.

Premature EXIT from Loop

Control flow jumps out of a loop after FETCH without invoking CLOSE in an exception path.

Implicit Statement Reuse

Some connectors reuse prepared statements across calls, leading to an unexpected open cursor state.

Nested Cursor Logic

Opening a second cursor on the same statement without closing the first leaves the original cursor open.

Related Errors

MySQL Error 1243: Unknown Prepared Statement Handler

Occurs when you try to CLOSE or DEALLOCATE a statement that was not prepared or was already unprepared.

MySQL Error 1329: No Data

Raised after a FETCH returns no rows; if you forget to CLOSE the cursor, executing again can lead to Error 1420.

MySQL Error 2014: Commands Out of Sync

Appears in client libraries when you issue a new query without reading the full result set of the previous statement.

FAQs

Does Error 1420 mean my data is lost?

No data is lost; the server simply blocks re-execution until the cursor is closed.

Can I auto-close cursors in MySQL?

MySQL does not auto-close cursors; you must call CLOSE explicitly or reset the prepared statement.

Is this error version specific?

Error 1420 exists in all supported MySQL 5.7 and 8.x versions that support cursors and prepared statements.

How does Galaxy help?

Galaxy highlights open cursors in the SQL editor and suggests CLOSE statements with its AI copilot, reducing the chance of hitting error 1420.

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