Skip to content

ABAP Keyword Documentation →  ABAP Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Views →  ABAP CDS - DDL Statements →  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 in ABAP CDS. Case distinction evaluates the sequence of conditions cond_expr1, cond_expr2, ... and returns the operand result as the result after THEN, for which the condition is true for the first time. 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.


Example

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

@AbapCatalog.sqlViewName: 'DEMO_CDS_SCASE'
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.