Core Data Services (CDS) ABAP

Associations

Associations

An association describes a directed connection to another DDIC or CDS object.

Example Scenario

SalesOrderItems

@AbapCatalog.sqlViewName: 'ZV_VDM_SO_I'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'SalesOrderItem'
define view zcds_vdm_so_i as 

select from snwd_so_i as soi

association [1..1] to zcds_vdm_so as _SalesOrder
on soi.parent_key = _SalesOrder.NodeKey

{
    key node_key as NodeKey,
    parent_key as ParentKey,
    so_item_pos as SoItemPos,
...
    currency_code as CurrencyCode,
    gross_amount as GrossAmount,
    net_amount as NetAmount,

    //Associations
    _SalesOrder
}

SalesOrder

@AbapCatalog.sqlViewName: 'ZV_VDM_SO'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'SalesOrder'
define view zcds_vdm_so as 

select from snwd_so as so

association [*] to zcds_vdm_so_i as _SalesOrderItem
on so.node_key = _SalesOrderItem.ParentKey

association [1..1] to zcds_vdm_bpa as _Buyer
on so.buyer_guid = _Buyer.NodeKey

{
    key node_key as NodeKey,
    so_id as SoId,
    created_by as CreatedBy,
...
    buy_contact_guid as BuyContactGuid,
    ship_to_adr_guid as ShipToAdrGuid,
    bill_to_adr_guid as BillToAdrGuid,

    // Associations
    _SalesOrderItem,
    _Buyer
}

Example Scenario from ABAP Perspective

ABAP Code

  METHOD if_oo_adt_classrun~main.

    select
    from zcds_vdm_so_i
    fields 
      \_SalesOrder-SoId,
      SoItemPos,
      NetAmount,
      CurrencyCode,
      \_SalesOrder-NetAmount as so_net_amount,
      \_SalesOrder\_Buyer-CompanyName

    into table @data(result)
    UP TO 100 rows .

    out->write( result ).
  ENDMETHOD.

Example Scenario from the Perspective of Other CDS Objects

Navigation with dot notation is possible across multiple hops.

Associations – Cardinality (Source Cardinality)

define view zcds_as_so
  as select from snwd_so as so
  association to snwd_bpa as _buyer on so.buyer_guid = _buyer.node_key
  association [1..*] to snwd_so_i as _items on so.node_key = _items.parent_key

How often does the associated target occur?

Notation: [<Min>..<Max>] or [Max]
Typical values:

  • 0
  • 1
  • * — any number of occurrences

The default value is [0..1].

If the cardinality is incorrect, the execution plan may not be well optimized or incorrect results may be calculated.

Associations – New Syntax for Source and Target Cardinality (since 2023)

As of S/4HANA 2023, cardinality can be expressed with text in addition to brackets:

[many | one | exact one ] to [ many | one | exact one ]

Since the new syntax provides more information, the execution plan can be better optimized in specific situations.
It is important that the information is correct — otherwise the SISO principle applies.

The new syntax works in:

  • ABAP CDS for:
    • JOIN
    • ASSOCIATION
    • Path expressions
  • ABAP SQL

SAP Blogpost

Associations – Path Expressions in CDS

_items[1:SoItemPos = '0000000010' ] as _first_item,              --Association to first position
_items.SoItemPos,                                                --All item numbers
_buyer.company_name,                                             --Name of buyer
_items[*:NetAmount > 1.00 ]._product.category as productCategory --Product categories of items priced > 1
_items[1: inner where SoItemPos = '0000000060' ].TaxAmount       --Tax amount of item 6

Path expressions can span multiple levels. For associations with multiple target objects, filters can be added in brackets.

Syntax:
[ <Cardinality> <JoinType> <Filter> ]
All components are optional.

  • Cardinality specifies the number of elements in the target
  • Join type can be INNER or LEFT OUTER
  • The filter contains conditions starting with WHERE

Associations – Path Expressions in ABAP

SELECT
FROM zcds_as_so AS soi
FIELDS \_ITEMS[  (*)  inner where NetAmount > 20000  ]\_PRODUCT-category
INTO  TABLE @DATA(lt_tmp).

Similar to CDS path expressions, but associations must start with ****.
The cardinality is specified in parentheses.

Exercise – Associations

Create two CDS View Entities using the following base tables:

  • ZBC_TASKS – Tasks
    • Field ASSIGNEE links to the responsible user
  • ZBC_USERS – Users
    • Field USER_ID corresponds to ASSIGNEE
  • Create an association from tasks to users
  • Create an association from users to tasks
  • Create an ABAP class implementing IF_OO_ADT_CLASSRUN that displays tasks with user first and last name.

Duration: 15 minutes