Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses →  SELECT →  SELECT - result →  SELECT - select_list →  SELECT - col_spec →  SELECT - sql_exp →  sql_exp - sql_case 

sql_exp - sql_searched_case

Other versions: 7.31 | 7.40 | 7.54

Syntax


... CASE WHEN sql_cond1 THEN result1 
        [WHEN sql_cond2 THEN result2]
        [WHEN sql_cond3 THEN result3]
          ...
        [ELSE resultn]
    END ...

Effect

Complex case distinction (searched case) in Open SQL. This SQL expression evaluates logical expressions sql_cond1, sql_cond2, ... in order and provides the operand result as a result after THEN. The logical expression is true for the first time for this result. If none of the logical expressions are true, the result specified after ELSE is selected. If ELSE is not specified, the result is the zero value.

The potential logical expressions sql_cond are a subset of the logical expressions sql_cond of a WHERE condition, but also enable SQL expressions to be used as operands. A logical expression sql_cond can be constructed from the following relational expressions, which can be joined using AND and OR and negated using NOT:

  • operand1 =|<>|>|<|>=|<= operand2
Compares the operand operand1 on the left side with the operand operand2 on the right side. The same applies to the relational operators as in a WHERE condition. The operand operand1 on the left side can be a column, a host variable, or a literal, but cannot be non-elementary SQL expression. The operand operand2 on the right side can be any SQL expression. If an operand of the comparison has the null value, the result of this comparison is unknown.
  • col BETWEEN dob1 AND dobj2
Checks the assignment to an interval. The relational expression functions in the same way as the corresponding WHERE condition. A column can be specified for col and host variables can be specified for dobj and dob2. If col has the null value, the result of the check is unknown.
  • col IS [NOT] NULL
Checks for the null value. The relational expression functions in the same way as the corresponding WHERE condition. A column can be specified for col.

The results result1, result2, ... can be columns, host variables, literals and any SQL expressions. Any columns and expressions that occur can have any dictionary types, except for ACCP, DF16_SCL (obsolete), DF34_SCL (obsolete), LCHR, LRAW, PREC, RAWSTRING, SSTRING, and STRING. Host variables and literals can have any ABAP types, except for string and xstring. Size comparisons and the operator BETWEEN, however, can only be specified for numeric types other than decimal floating point numbers.

It must be possible to compare the data types of the operands of a relational expression. If this is not the case, a statically specified type raises a syntax error and a dynamically specified type raises an exception of the class CX_SY_DYNAMIC_OSQL_SEMANTICS. The results must also be compatible to produce a common result type: Data types result1, result2, ... must be either the same or the data type must be able to fully represent the value of all other data types. The result has the dictionary type of the entry with the greatest value range.

The same applies to joins between comparisons with unknown results as to the WHERE condition.


Note

When a complex CASE is used, the syntax check is performed in a strict mode, which handles the statement more strictly than the regular syntax check.