SQL Reference

CASE Statement

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

Syntax

>>-CASE----+-| searched-case-statement-when-clause |-+---------->
           '-| simple-case-statement-when-clause |---'
 
>----END CASE--------------------------------------------------><
 
simple-case-statement-when-clause
 
|---expression-------------------------------------------------->
 
      .-------------------------------------------------------------.
      |                          .-------------------------------.  |
      V                          V                               |  |
>--------WHEN--expression--THEN-----SQL-procedure-statement--;---+--+>
 
>-----+------------------------------------------+--------------|
      |       .-------------------------------.  |
      |       V                               |  |
      '-ELSE-----SQL-procedure-statement--;---+--'
 
searched-case-statement-when-clause
 
    .-------------------------------------------------------------------.
    |                                .-------------------------------.  |
    V                                V                               |  |
|------WHEN--search-condition--THEN-----SQL-procedure-statement--;---+--+->
 
>----+------------------------------------------+---------------|
     |       .-------------------------------.  |
     |       V                               |  |
     '-ELSE-----SQL-procedure-statement--;---+--'
 

Description

CASE
Begins a case-expression.

simple-case-statement-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 search condition is true, the THEN statement is executed. If the result is unknown or false, processing continues to the next search condition. If the result does not match any of the search conditions, and an ELSE clause is present, the statements in the ELSE clause are processed.

searched-case-statement-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.

SQL-procedure-statement
Specifies a statement that should be invoked.

END CASE
Ends a case-statement.

Notes

Examples

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-case-statement-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

The following example shows how to do this using the syntax for a searched-case-statement-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 ]