Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   SPL Statements >

CASE

Use the CASE statement when you need to take one of many branches depending on the value of an SPL variable or a simple expression. The CASE statement is a fast alternative to the IF statement.

Only Extended Parallel Server supports the CASE statement.

Syntax

Read syntax diagramSkip visual syntax diagram>>-CASE--value_expr--------------------------------------------->
 
                               (1)
>--+-ELSE--| Statement Block |-------------------------------------------------------------------+-->
   | .-----------------------------------------------------.                                     |
   | V                                                (1)  |                                     |
   '---WHEN--constant_expr--THEN--| Statement Block |------+--+--------------------------------+-'
                                                              |                           (1)  |
                                                              '-ELSE--| Statement Block |------'
 
>--END CASE----------------------------------------------------><
 
Notes:
  1. See Statement Block

Element Description Restrictions Syntax
constant_expr Expression that specifies a literal value Can be a literal number, quoted string, literal datetime, or literal interval. The data type must be compatible with the data type of value_expr. Constant Expressions
value_expr Expression that returns a value An SPL variable or any other type of expression that returns a value Expression

Usage

You can use the CASE statement to create a set of conditional branches within an SPL routine. The WHEN and the ELSE clauses are optional, but you must include at least one of them. If you specify no WHEN and no ELSE clause, you receive a syntax error.

3Do not confuse the CASE statement with CASE expressions 3of SQL. (For Extended Parallel Server, CASE expressions support the same keywords 3as the CASE statement, but use different syntax and semantics to evaluate conditions that you specify and return a single value 3or NULL, as described in CASE Expressions.)

How the Database Server Executes a CASE Statement

The database server executes the CASE statement in the following way:

The statement block that follows the THEN or ELSE keywords can include any SQL statement or SPL statement that is valid in the statement block of an SPL routine. For more information, see Statement Block.

Computation of the Value Expression in CASE

The database server computes the value of the value_expr parameter only one time. It computes this value at the start of execution of the CASE statement. If the value expression specified in the value_expr parameter contains SPL variables and the values of these variables change subsequently in one of the statement blocks within the CASE statement, the database server does not recompute the value of the value_expr parameter. So a change in the value of any variables contained in the value_expr parameter has no effect on the branch taken by the CASE statement.

Example of CASE Statement

In the following example, the CASE statement initializes one of a set of SPL variables (named j, k, l, and m) to the value of an SPL variable named x, depending on the value of another SPL variable named i:

CASE i
   WHEN 1 THEN LET j = x;
   WHEN 2 THEN LET k = x;
   WHEN 3 THEN LET l = x;
   WHEN 4 THEN LET m = x;
   ELSE
      RAISE EXCEPTION 100; --illegal value
END CASE

Related Statements

IF

Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]