MySQL raises error 1323 when a cursor is declared or opened on a SELECT statement that contains an INTO clause inside a stored routine.
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.
Cursor SELECT must not have INTO
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.
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.
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.
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.
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.
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.
Adding INTO directly to the SELECT used to declare or open the cursor triggers error 1323 immediately.
Automated tools or ORMs may inject INTO clauses when creating stored routines, leading to invalid cursor SQL.
Developers often paste a tested SELECT ... INTO query into a cursor declaration without removing the INTO portion.
Raised when a cursor SELECT includes FOR UPDATE. Remove the locking clause or use SELECT without FOR UPDATE.
Occurs when you OPEN or FETCH a cursor after a HANDLER has been declared. Reorder statements or close the handler first.
Triggered when cursor statements appear after DECLARE variables. Place cursor declarations before variable declarations.
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.
No. Both versions enforce the same rule: cursor SELECT statements must not contain INTO clauses.
Galaxys real-time linting highlights invalid cursor syntax as you type. Team reviews in Collections let peers catch INTO clauses before code reaches production.
No. Using FETCH ... INTO performs variable assignment row by row, which is the intended cursor workflow and has negligible overhead.