Project: YugabyteDB (App Definition and Development / Database)
File: case-statement.md

title: The "case" statement [YSQL] headerTitle: The "case" statement linkTitle: The "case" statement description: Describes the syntax and semantics of the PL/pgSQL "case" statement. [YSQL]. menu: stable: identifier: case-statement parent: compound-statements weight: 30 type: docs showRightNav: true {{%ebnf%}} plpgsqlcasestmt, plpgsqlsearchedcase_stmt, plpgsqlsearchedwhen_leg, plpgsqlsimplecase_stmt, plpgsqlsimplewhen_leg {{%/ebnf%}} Just like the , the case statement lets you specify one or several lists of executable statements so that a maximum of one of those lists will be selected. Each list is guarded by a boolean guardexpression_and each is tested in turn, in the order in which they are written. The guardexpression_ is written: in the searched case statement, explicitly as an boolean expression in each plpgsqlsearchedwhenleg_ in the simple case statement, implicitly by writing the expression on the left hand side of the implied equality operator immediately after the case keyword that introduces the statement and by writing the expression on the right hand side of the implied equality operator immediately after the when keyword in each plpgsqlsimplewhenleg_. When a guardexpression evaluates to false, the point of execution immediately moves to the next guardexpression, skipping the statement list that it guards. As soon as a guardexpression evaluates to true, the statement list that it guards is executed; and on completion of that list, control passes to the first statement after the end case of the case statementskipping the evaluation of any remaining guardexpressions. This economical testing of the guardexpressions_ is common to many programming languages. It is a particular example of so-called short-circuit evaluation. Critically, and in contrast to the if statement's semantics, the 20000 (case not found) error occurs if every guard expression (whether explicit or implicit) evaluates to false. Here is the template of the simplest case statement: ```plpgsql case when <guard_expression> then <guarded statement list> end case; ``` However, this degenerate form is not useful because it has the same effect as this: ```plpgsql assert <guard_expression>; <guarded statement list> ``` In other words, an exception is guaranteed unless the guardexpression evaluates to true_. The two-leg form of the simple case statement is useful when the target expression is a boolean variable that (if the larger context is bug-free) will always be not null: ```plpgsql create procedure s.p(b in boolean) set searchpath = pgcatalog, pg_temp language plpgsql as $body$ begin case b when true then <some actions> when false then <some alternative actions> end case; end; $body$; ``` Try this counter example: ```plpgsql call s.p(null); ``` This causes the 20000 error: ```output ERROR: case not found HINT: CASE statement is missing ELSE part. CONTEXT: PL/pgSQL function s.p(boolean) line 3 at CASE ``` honoring the defined case statement semantics. A case statement can always be rewritten as an if statement. But care must be taken to implement the case not found semantics when the to-be-rewritten case statement doesn't have a bare else branch. Programmers argue about their preferences for the choice between a case statement and an if statement. See at the send of the if statement page.