Common SQL Errors

MySQL Error 1323 ER_SP_BAD_CURSOR_SELECT: Cursor SELECT must not have INTO - How to Fix and Prevent

Galaxy Team
August 6, 2025

MySQL raises error 1323 when a cursor is declared or opened on a SELECT statement that contains an INTO clause inside a stored routine.

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 1323 (ER_SP_BAD_CURSOR_SELECT)?

MySQL Error 1323 ER_SP_BAD_CURSOR_SELECT appears when you declare or open a cursor on a SELECT that includes an INTO clause inside a stored program. Use a plain SELECT for the cursor and perform variable assignments with FETCH or a separate SELECT to resolve the issue.

Error Highlights

Typical Error Message

Cursor SELECT must not have INTO

Error Type

Cursor Error

Language

MySQL

Symbol

ER_SP_BAD_CURSOR_SELECT

Error Code

1323

SQL State

42000

Explanation

Table of Contents

What is MySQL error 1323 (ER_SP_BAD_CURSOR_SELECT)?

MySQL error 1323 is triggered inside stored procedures, functions, or triggers when you attempt to declare or open a cursor on a SELECT statement that also contains an INTO clause.

The server expects the cursor query to produce a result set. Adding INTO turns the statement into a variable-assignment operation, so the parser stops execution and returns SQLSTATE 42000.

What Causes This Error?

The primary cause is placing INTO var_list or INTO @user_variable directly in the SELECT that defines the cursor. MySQL treats this as invalid cursor syntax.

The error can also arise when dynamic SQL or code generators insert the INTO clause automatically, or when developers copy an ad-hoc SELECT ... INTO into a stored routine without modification.

How to Fix MySQL Error 1323 ER_SP_BAD_CURSOR_SELECT

Rewrite the cursor definition so that the SELECT returns rows with no INTO clause. Capture column values after each FETCH inside the loop.

If you need to set variables before processing, run a separate SELECT ... INTO statement outside the cursor, or use FETCH INTO within the loop to populate local variables.

Common Scenarios and Solutions

Copy-pasting a SELECT ... INTO query from testing into a CURSOR declaration is the most frequent scenario. Removing INTO immediately resolves the issue.

In generated migration scripts, ORMs may append INTO automatically. Disable that feature or post-process the SQL to keep cursor queries clean.

Best Practices to Avoid This Error

Always separate cursor queries from variable assignments. Use FETCH ... INTO for assignments after opening the cursor.

Run stored-routine unit tests in Galaxy or another IDE before deployment. Strict SQL_MODE settings help surface the problem early.

Related Errors and Solutions

ER_SP_BAD_CURSOR_SELECT (1324) - Cursor SELECT contains FOR UPDATE. Solution: remove unsupported clause.

ER_SP_CURSOR_AFTER_HANDLER (1325) - OPEN or FETCH after a handler illegal. Ensure handlers close cursors properly.

Common Causes

Including INTO in cursor SELECT

Adding INTO directly to the SELECT used to declare or open the cursor triggers error 1323 immediately.

Code generation mistakes

Automated tools or ORMs may inject INTO clauses when creating stored routines, leading to invalid cursor SQL.

Copy-paste refactoring errors

Developers often paste a tested SELECT ... INTO query into a cursor declaration without removing the INTO portion.

Related Errors

MySQL Error 1324 ER_SP_BAD_CURSOR_SELECT

Raised when a cursor SELECT includes FOR UPDATE. Remove the locking clause or use SELECT without FOR UPDATE.

MySQL Error 1325 ER_SP_CURSOR_AFTER_HANDLER

Occurs when you OPEN or FETCH a cursor after a HANDLER has been declared. Reorder statements or close the handler first.

MySQL Error 1326 ER_SP_CURSOR_AFTER_GLOBALS

Triggered when cursor statements appear after DECLARE variables. Place cursor declarations before variable declarations.

FAQs

Can I ever use SELECT ... INTO with cursors?

You cannot include INTO in the cursor definition, but you can use FETCH ... INTO inside the loop or run a separate SELECT ... INTO before opening the cursor.

Does the error differ between MySQL 5.7 and 8.0?

No. Both versions enforce the same rule: cursor SELECT statements must not contain INTO clauses.

How can Galaxy help avoid this error?

Galaxys real-time linting highlights invalid cursor syntax as you type. Team reviews in Collections let peers catch INTO clauses before code reaches production.

Will removing INTO impact performance?

No. Using FETCH ... INTO performs variable assignment row by row, which is the intended cursor workflow and has negligible overhead.

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