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:


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:


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:


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:


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:


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:

The CASE expression is versatile and can be used in multiple scenarios to meet different requirements for dynamic field values based on conditions.