1331
MySQLerrorstored-procedureshigh confidence

Case not found for CASE statement

Production Risk

Low — runtime error inside stored routine; calling statement is rolled back.

What this means

A CASE statement in a stored routine found no matching WHEN clause and no ELSE clause was provided.

Why it happens
  1. 1CASE value did not match any WHEN and there is no ELSE
  2. 2Unexpected NULL value — NULL does not match any WHEN without explicit WHEN NULL
How to reproduce
trigger — this will error
trigger — this will error
CREATE PROCEDURE p(x INT) BEGIN CASE x WHEN 1 THEN SELECT 'one'; END CASE; END; CALL p(99);

expected output

ERROR 1331 (20000): Case not found for CASE statement

Fix

Add an ELSE clause

Add an ELSE clause
CASE x WHEN 1 THEN SELECT 'one'; ELSE SELECT 'other'; END CASE;

Why this works

Provides a default branch for unmatched values.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 1331 ER_SP_CASE_NOT_FOUND

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors