SAP ABAP CDS View – Examples*
SAP Core Data Services (CDS) Views help define data models at the database level, improving performance and simplifying data access. Below are different examples covering various scenarios.
1️⃣ Basic CDS View
@AbapCatalog.sqlViewName: 'ZFLIGHT_CDS'@AccessControl.authorizationCheck: #NOT_REQUIREDdefine view ZFLIGHT_CDS as select from sflight { key carrid, key connid, fldate, price}
✅ Retrieves basic flight details from the sflight
table.
2️⃣ CDS View with Joins (Associations)
define view ZFLIGHT_DETAILS as select from sflightassociation [0..1] to scarr as _Carrier on _Carrier.carrid = sflight.carrid{ key sflight.carrid, key sflight.connid, sflight.fldate, sflight.price, _Carrier.name as airline_name}
✅ Retrieves flight details along with the airline name using an association.
3️⃣ CDS View with CASE WHEN (Conditional Logic)
define view ZFLIGHT_CATEGORY as select from sflight { key carrid, key connid, fldate, case when price > 500 then 'Expensive' when price > 200 then 'Moderate' else 'Cheap' end as price_category}
✅ Categorizes flights as Expensive, Moderate, or Cheap based on price.
4️⃣ CDS View with String Concatenation
define view ZFLIGHT_INFO as select from sflight { key carrid, key connid, fldate, carrid || ' - ' || connid as flight_number}
✅ Combines carrid
and connid
into a single flight number.
5️⃣ CDS View with Current Date Filter
define view ZFLIGHT_UPCOMING as select from sflightwhere fldate >= SESSION_CONTEXT('CDSSESSIONDATE') { key carrid, key connid, fldate}
✅ Retrieves only upcoming flights by filtering records based on the current date.
6️⃣ CDS View with Aggregations (SUM, COUNT)
define view ZFLIGHT_STATS as select from sflight { carrid, count( * ) as total_flights, sum( price ) as total_revenue}group by carrid
✅ Groups flights by carrier (carrid
) and calculates total flights and revenue.
7️⃣ CDS View with Type Conversion (CAST)
define view ZFLIGHT_CAST as select from sflight { key carrid, key connid, cast( fldate as abap.char(10) ) as flight_date_text}
✅ Converts fldate
(DATE) to CHAR(10) for text-based processing.
8️⃣ CDS View Using DEFINE VIEW ENTITY
(Modern Approach)
@AccessControl.authorizationCheck: #CHECKdefine view entity ZFLIGHT_ENTITY as select from sflight { key carrid, key connid, fldate, price}
✅ Uses DEFINE VIEW ENTITY
for better performance and lifecycle management (Recommended for S/4HANA).
💡 Key Takeaways
✅ CDS Views simplify data retrieval and improve performance.
✅ Use Associations for efficient joins instead of traditional SQL joins.
✅ Use CASE WHEN, CAST, and CONCATENATION for dynamic data processing.
✅ DEFINE VIEW ENTITY
is recommended for new developments.