case not found
SQLSTATE 20000 is raised when a PL/pgSQL CASE statement does not find a matching WHEN clause and no ELSE clause is provided. The statement terminates with an error.
- 1PL/pgSQL CASE statement with no matching WHEN clause and no ELSE
- 2A searched CASE expression encounters a value not covered by any WHEN
CASE statement with no ELSE for an unhandled value.
DO $
DECLARE v INT := 99;
BEGIN
CASE v
WHEN 1 THEN RAISE NOTICE 'one';
WHEN 2 THEN RAISE NOTICE 'two';
-- no ELSE, 99 not covered
END CASE;
END $;expected output
ERROR: case not found HINT: CASE statement is missing ELSE part.
Fix 1
Add an ELSE clause to the CASE statement
WHEN When the CASE must handle all possible values.
CASE v WHEN 1 THEN RAISE NOTICE 'one'; WHEN 2 THEN RAISE NOTICE 'two'; ELSE RAISE NOTICE 'other: %', v; END CASE;
Why this works
An ELSE clause provides a fallback for any value not matched by a WHEN, preventing the 20000 error.
Fix 2
Raise a meaningful exception in the ELSE clause
WHEN When an unmatched value represents a programming error.
ELSE RAISE EXCEPTION 'Unexpected value: %', v USING ERRCODE = 'P0001';
Why this works
Raising explicitly in ELSE gives a descriptive error rather than the generic 20000.
Class 20 — Case Not Found
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev