ABAP - Keyword Documentation →  ABAP - Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Data Definitions →  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

Syntax

... CASE WHEN cond_expr1 THEN result1
        [WHEN cond_expr2 THENresult2]
        [WHEN cond_expr3 THENresult3]
          ...
        [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'
@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'

Das Programm DEMO_CDS_SEARCHED_CASE greift mit SELECT auf die View zu und stellt das Ergebnis dar.