CASE Statement

The CASE statement is a control statement that lets you choose a list of SQL statements to execute based on the value of an expression.

Quick Links:

Go to Examples

Go to Usage

Go to Standards

Go to Permissions

Syntax

CASE value-expressionWHENconstant | NULL ] THEN statement-list …
   … [ WHENconstant | NULL ] THEN statement-list ] …
   …ELSE statement-listEND

Examples

(back to top)

Usage

(back to top)

If a WHEN clause exists for the value of value-expression, the statement-list in the WHEN clause is executed. If no appropriate WHEN clause exists, and an ELSE clause exists, the statement-list in the ELSE clause is executed. Execution resumes at the first statement after the END.

Note: The ANSI standard allows two forms of CASE statements. Although SAP Sybase IQ allows both forms, when CASE is in the predicate, for best performance you must use the form shown here.

If you require the other form (also called ANSI syntax) for compatibility with SQL Anywhere, use this syntax:

CASE
   WHEN [ search-condition | NULL] THEN statement-list ...
   [ WHEN [ search-condition | NULL] THEN statement-list ] ...
   [ ELSE statement-list ]
   END [ CASE ]

With this ANSI syntax form, the statements are executed for the first satisfied search-condition in the CASE statement. The ELSE clause is executed if none of the search-conditions are met. If the expression can be NULL, use the following syntax for the first search-condition:

WHEN search-condition IS NULL THEN statement-list
Attention: Do not confuse the syntax of the CASE statement with that of the CASE expression.

Standards

(back to top)

  • SQL—Vendor extension to ISO/ANSI SQL grammar.
  • SAP Sybase Database product—Not supported by Adaptive Server.

Permissions

(back to top)

None

Related reference
BEGIN … END Statement