CASE statement

The CASE statement selects an execution path based on multiple conditions.

Syntax

Click to skip syntax diagram
Read syntax diagramSkip visual syntax diagram>>-+--------+--CASE--+-simple-when-clause---+------------------->
   '-label:-'        '-searched-when-clause-'
 
>--+-------------+--END CASE-----------------------------------><
   '-else-clause-'
 
simple-when-clause:
 
|--expression--------------------------------------------------->
 
   .---------------------------------------------------------.
   |                         .-----------------------------. |
   V                         V                             | |
>----WHEN--expression--THEN----SQL-procedure-statement-- ;-+-+--|
 
searched-when-clause:
 
   .---------------------------------------------------------------.
   |                               .-----------------------------. |
   V                               V                             | |
|----WHEN--search-condition--THEN----SQL-procedure-statement-- ;-+-+--|
 
else-clause:
 
         .-----------------------------.
         V                             |
|--ELSE----SQL-procedure-statement-- ;-+------------------------|
 

Description

label
Specifies the label for the CASE statement. The label name cannot be the same as another label within the same scope. For more information, see Labels.
simple-when-clause
The value of the expression prior to the first WHEN keyword is tested for equality with the value of each expression that follows the WHEN keyword. If the comparison is true, the THEN statement is executed. If the result is unknown or false, processing continues to the next comparison. If the result does not match any of the comparisons, and an ELSE clause is present, the statements in the ELSE clause are processed.
searched-when-clause
The search-condition following the WHEN keyword is evaluated. If it evaluates to true, the statements in the associated THEN clause are processed. If it evaluates to false, or unknown, the next search-condition is evaluated. If no search-condition evaluates to true and an ELSE clause is present, the statements in the ELSE clause are processed.
else-clause
If none of the conditions specified in the simple-when-clause or searched-when-clause are true, then the statements in the else-clause are executed.

If none of the conditions specified in the WHEN are true, and an ELSE clause is not specified, an error is issued at runtime, and the execution of the CASE statement is terminated (SQLSTATE 20000).

SQL-procedure-statement
Specifies a statement that should be executed. See SQL-procedure-statement.

Notes

Nesting of CASE statements: CASE statements that use a simple-when-clause can be nested up to three levels. CASE statements that use a searched-when-clause have no limit to the number of nesting levels.

Examples

Example 1: Depending on the value of SQL variable v_workdept, update column DEPTNAME in table DEPARTMENT with the appropriate name.

The following example shows how to do this using the syntax for a simple-when-clause.

               CASE v_workdept
                 WHEN 'A00'
                   THEN UPDATE department SET
                              deptname = 'DATA ACCESS 1';
                 WHEN 'B01'
                   THEN UPDATE department SET
                              deptname = 'DATA ACCESS 2';
                   ELSE UPDATE department SET
                              deptname = 'DATA ACCESS 3';
               END CASE 

Example 2: The following example shows how to do this using the syntax for a searched-when-clause:

               CASE
                 WHEN v_workdept = 'A00'
                   THEN UPDATE department SET
                              deptname = 'DATA ACCESS 1';
                 WHEN v_workdept = 'B01'
                   THEN UPDATE department SET
                              deptname = 'DATA ACCESS 2';
                   ELSE UPDATE department SET
                              deptname = 'DATA ACCESS 3';
               END CASE


[ Top of Page | Previous Page | Next Page | Contents | Index ]