Saturday, December 5, 2020

Case Expression in CDS View

 

ABAP CDS - case_expr

Syntax Forms


Simple case distinction

1. ... CASE operand
           WHEN operand1 THEN result1
          [WHEN operand2 THEN result2]
           ...
          [ELSE resultn]
      END ...


Complex case distinction

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

Effect

Case distinction in a SELECT statement of a CDS view in ABAP CDS. Either a simple case distinction (simple case) or a complex case distinction (searched case).

Case distinctions can be specified in the SELECT list and in operand positions of other expressions. 

 Simple Case Expression:

Case distinction in a SELECT list.

case partner.bp_role
    when '01' then 'customer'
    when '02' then 'supplier'
end as partner_role
 
 
 
 
 Complex case distinction :
 

 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.

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SCASE'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view Zdemo_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 = 'AI'