SAP ABAP CDS View Association

Associations in ABAP Core Data Services (CDS) views define relationships between different database tables or views, similar to joins but with better performance and flexibility.


🔹 Basic Syntax of an Association

define view ZDEMO_CDS as select from sflight
association [0..1] to scarr as _Carrier on _Carrier.carrid = sflight.carrid
{
key sflight.carrid,
key sflight.connid,
sflight.fldate,
_Carrier.name
}

🔹 Explanation:

✅ Defines an association (_Carrier) to the scarr table. ✅ Uses ON condition (_Carrier.carrid = sflight.carrid). ✅ Selects fields from both sflight and the associated _Carrier. ✅ The [0..1] cardinality means at most one matching record.


🔹 Types of Associations

TypeExampleMeaning
[0..1]association [0..1] to scarrAt most one match (like LEFT JOIN with a single record).
[1..1]association [1..1] to scarrExactly one matching record.
[0..*]association [0..*] to spfliMultiple matches (like LEFT JOIN with many records).

🔹 Using Associations in SELECT Queries

Associations are lazy by default (not included in the result). To retrieve data from an association, use the INNER JOIN keyword:

select from ZDEMO_CDS
inner join _Carrier
fields carrid, connid, fldate, _Carrier.name
into table @DATA(result).

This explicitly fetches _Carrier.name from the associated table.


🔹 Benefits of Associations

✅ Better Performance – More efficient than traditional JOINs. ✅ Reusability – Defined once and used across multiple views. ✅ Lazy Loading – Only fetched when explicitly used in a query.

Would you like a more advanced example with multiple associations? 🚀