This form of the CASE statement executes a list of statements when the conditional expression in the WHEN clause evaluates to true. You can execute the statements associated with at most one WHEN clause or ELSE clause.
The WHEN clauses are evaluated in the order in which they are specified in the CASE statement. The process of evaluation is as follows:
- The first WHEN clause is evaluated.
- If the conditional expression specified in the WHEN clause is true, the statements of that WHEN clause are executed.
- Control moves to the next statement in the stored procedure.
If the conditional expression is not true, then the next WHEN clause, if it exists, is evaluated.
- All subsequent WHEN clauses are evaluated as described in stage 1.
- When there are no more WHEN clauses to evaluate, the ELSE clause, if exists, is taken up and the statements of the ELSE clause are executed.
Control moves to the next statement in the stored procedure.
- If there is no ELSE clause and the conditional expression in none of the WHEN clauses evaluates to true,
- a runtime exception (“Case not found for CASE statement”, SQLSTATE=’20000’, SQLCODE = 7601) occurs.
- the execution of the CASE statement is terminated.