SAP ABAP CDS View – CASE WHEN Statement
In ABAP CDS Views, the CASE WHEN statement is used for conditional logic, similar to SQL CASE. It helps derive calculated fields based on conditions.
🔹 Basic Syntax of CASE WHEN in CDS
define view ZDEMO_CDS as select from sflight { key carrid, key connid, fldate, price, case when price > 500 then 'Expensive' when price > 200 then 'Moderate' else 'Cheap' end as price_category}🔹 Explanation:
✅ Assigns a category (Expensive, Moderate, Cheap) based on price.
✅ Works similarly to IF-ELSE logic in ABAP.
🔹 Using CASE with Calculations
define view ZFLIGHT_DISCOUNT as select from sflight { key carrid, key connid, price, case when price > 500 then price * 0.9 // 10% discount when price > 200 then price * 0.95 // 5% discount else price end as discounted_price}🔹 This applies dynamic price discounts based on conditions.
🔹 Using CASE with Boolean Flags
define view ZFLIGHT_STATUS as select from sflight { key carrid, key connid, fldate, case cancelled when 'X' then 'Cancelled' else 'Active' end as flight_status}🔹 Converts binary flag (X/Space) into readable values.
🔹 Key Benefits of CASE WHEN in CDS
✅ Simplifies conditional logic inside the CDS view. ✅ Improves performance by processing conditions at the database level. ✅ Enhances readability by converting flags into meaningful text.