Skip to content

ABAP Keyword Documentation →  ABAP - Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Data Definitions →  ABAP CDS - DDL for Data Definitions →  ABAP CDS - DEFINE VIEW →  ABAP CDS - SELECT →  ABAP CDS - SELECT, Operands and Expressions →  ABAP CDS - case_expr 

ABAP CDS - searched_case_expr

Other versions: 7.31 | 7.40 | 7.54

Syntax


... CASE WHEN cond_expr1 THEN result1 
        [WHEN cond_expr2 THEN result2]
        [WHEN cond_expr3 THEN result3]
          ...
        [ELSE resultn]
    END ...

Effect

Complex case distinction (searched case) in a SELECT statement of a CDS view. The case distinction evaluates the conditions cond_expr1, cond_expr2, ... and returns the operand result as the result after the first THEN for which the condition is true. If none of the conditions are true, the result specified after ELSE is selected. If ELSE is not specified, the result is the zero value. Special rules apply when specifying the conditions. The same applies for result1, result2, ... as for simple case distinction.


Notes

  • The SQL standard dictates the result of a case distinction, but not the order in which the operands are evaluated. Potentially, the result may even be evaluated before the associated condition. This means that any expressions specified as operands must have no side effects and must not be dependent on each other.

  • On the SAP HANA database, operands are evaluated in parallel for reasons of optimization. The order in which the operands are evaluated is undefined. If an exception is raised when am operand is evaluated, the entire case distinction is canceled. Here, it does not matter which conditions apply and the order in which they are noted. From this reason, it is advisable not to use any exceptions in expressions specified as operands. More information can be found in the HANA-specific SQL documentation.

Example

The following CDS view has a complex case distinction in the SELECT list.

@AbapCatalog.sqlViewName: 'DEMO_CDS_SCASE'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_searched_case
as select from
spfli
{
key carrid,
key connid,
distance,
distid,
case
when distance >= 2000 then 'long-haul flight'
when distance >= 1000 and
distance < 2000 then 'medium-haul flight'
when distance < 1000 then 'short-haul flight'
else 'error'
end as flight_type
}
where
distid = 'MI'    

The program DEMO_CDS_SEARCHED_CASE uses SELECT to access the view and shows the result.