In SAP ABAP CDS Views, you can use conditional logic to implement scenarios like IF conditions. The typical way to express conditional logic in a CDS view is through the CASE expression, as ABAP’s IF statements are not directly supported in CDS views.
Here’s how you can implement conditional logic using CASE and IF-ELSE scenarios in CDS views.
1. Using CASE Expression in CDS View
The CASE expression in CDS allows you to handle conditional logic, which is similar to the IF statement in ABAP. You can use it to conditionally evaluate columns or values.
Syntax of CASE Expression
CASE <condition> WHEN <value1> THEN <result1> WHEN <value2> THEN <result2> ELSE <default_result>END AS <new_field_name>Example 1: Basic CASE Expression
define view ZFLIGHT_CONDITION_CASE as select from sflight { key carrid, key connid, fldate, price, // CASE statement to apply conditions case when price > 500 then 'Expensive' when price <= 500 and price > 100 then 'Moderate' else 'Cheap' end as price_category}Explanation:
- This example classifies the flight’s price into three categories (
Expensive,Moderate,Cheap) based on the value of thepricefield. - The
CASEexpression evaluates thepricefield and assigns a corresponding category.
2. Using CASE with Multiple Conditions (Nested CASE)
You can also nest multiple conditions using a CASE statement for more complex logic, similar to IF-ELSEIF chains in ABAP.
Example 2: Nested CASE Expression
define view ZFLIGHT_PRICE_CATEGORY as select from sflight { key carrid, key connid, fldate, price, // Nested CASE expression to check multiple conditions case when price > 1000 then 'Premium' when price > 500 then case when price > 750 then 'High Moderate' else 'Low Moderate' end else 'Economy' end as price_segment}Explanation:
- This nested
CASEstatement applies a more detailed classification of flight prices into four categories:Premium,High Moderate,Low Moderate, andEconomy. - The logic checks the price and classifies it accordingly, with nested
CASEstatements.
3. Using COALESCE for Null Handling
In some cases, you may want to handle NULL values in your fields. The COALESCE function can be used to replace NULL values with a default value.
Example 3: Using COALESCE for Default Values
define view ZFLIGHT_COALESCE as select from sflight { key carrid, key connid, fldate, price, // Use COALESCE to replace NULL values with a default value coalesce( price, 0 ) as price_with_default}Explanation:
- The
COALESCEfunction checks if thepriceisNULL. If it is, it replaces the value with0(default value).
4. Conditional Logic with Boolean Expressions
You can also use Boolean expressions in a CASE or SELECT clause for conditions based on boolean values.
Example 4: Using Boolean Expressions in CASE
define view ZFLIGHT_BOOLEAN_CONDITION as select from sflight { key carrid, key connid, fldate, price, // Using a boolean expression in a CASE statement case when price > 500 then true else false end as is_expensive}Explanation:
- In this example, a boolean value (
trueorfalse) is assigned to the fieldis_expensivebased on the condition that thepriceis greater than500.
5. Using IF-like Logic with CASE Expression
While IF statements themselves are not allowed in CDS, you can implement equivalent IF-ELSE logic using CASE for various scenarios.
Example 5: Emulating IF-ELSE Logic in CDS View
define view ZFLIGHT_IF_CONDITION as select from sflight { key carrid, key connid, fldate, price, // Emulating IF-ELSE logic using CASE case when price > 1000 then 'Premium Fare' else 'Standard Fare' end as fare_type}Explanation:
- This example mimics an
IF-ELSElogic where if thepriceis greater than1000, thefare_typeisPremium Fare. Otherwise, it assigns the valueStandard Fare.
Conclusion
In SAP ABAP CDS Views, conditional logic can be implemented using the CASE expression. This allows you to replicate IF-like behavior with the following:
- Simple
CASEexpressions to check conditions and assign results. - Nested
CASEexpressions for more complex conditional logic. - Boolean expressions for handling true/false values.
- COALESCE to handle
NULLvalues with default replacements.
The CASE expression is versatile and can be used in multiple scenarios to meet different requirements for dynamic field values based on conditions.