CASE Statement

Selects execution path based on multiple cases.

Syntax

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

Examples

Usage

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.

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 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, see the CASE statement Syntax 2 in SQL Anywhere 11.0.1 > SQL Anywhere Server – SQL Reference > Using SQL > SQL statements > SQL statements (A-D) > CASE statement.

Attention:

Do not confuse the syntax of the CASE statement with that of the CASE expression.

For information on the CASE expression, see Reference: Building Blocks, Tables, and Procedures > SQL Language Elements > Expressions.

Standards

  • SQL—Vendor extension to ISO/ANSI SQL grammar.

  • Sybase—Not supported by Adaptive Server Enterprise.

Permissions

None

Related reference
BEGIN … END Statement