CDS Views With Association
CDS Views With Association
Prerequisites
Install ABAP Development Tools (Eclipse or HANA Studio)
Create ABAP CDS View.
Step-by-step Procedure
Before starting our development lets understand about the background of
associations and its usability.
What is an Association?
Association is a relationship between two CDS views.
1. Create a CDS View to get the sales order header information from the
database table (VBAK). Below is the sample DDL code snippet for the
same.
@AbapCatalog.sqlViewName: 'ZV_ORD_HDR'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
vbeln,
erdat,
vbtyp,
auart,
vkorg,
vtweg,
spart,
netwr,
waerk
}
2. Create another CDS view to get the sales order item information from the
database table (VBAP). Below is the sample DDL code snippet for the same.
@AbapCatalog.sqlViewName: 'ZV_ORD_ITM'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
vbeln,
posnr,
matnr,
zwert,
zmeng
the above CDS view seems to simple select from single table, let's make it
complex by adding JOINS to get more details.
@AbapCatalog.sqlViewName: 'ZV_ORD_ITM'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
vbeln,
posnr,
vbap.matnr,
makt.maktx,
zwert,
zmeng
@AbapCatalog.sqlViewName: 'ZV_ORD_HDR'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
on vbak.vbeln = _OrderItems.vbeln
{
key vbeln,
erdat,
vbtyp,
auart,
vkorg,
vtweg,
spart,
netwr,
waerk,
_OrderItems.matnr,
_OrderItems.maktx
}
Association syntax on another CDS view ZCDS_SALESORDER_ITM based on the
VBELN (sales order number) field.
Fields from the association can be accessed in the SELECT list by prefixing them
with association name followed by period (.) _OrderItems.matnr
@AbapCatalog.sqlViewName: 'ZV_ORD_HDR'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
on vbak.vbeln = _OrderItems.vbeln
key vbeln,
erdat,
vbtyp,
auart,
vkorg,
vtweg,
spart,
netwr,
waerk,
_OrderItems.matnr,
_OrderItems.maktx
}
Below are the rules for min and max values.
max cannot be 0.
An asterisk * for max means any number of rows.
min can be omitted (set to 0 if omitted).
min cannot be *.
When an association is used in a WHERE condition, 1 must be specified
for max.
Association ON condition rules
When specifying the ON condition with association, following rules are applied.
on $projection.vbeln = _OrderItems.vbeln
{ ..... }
Data Preview
Right click on the DDL editor
Output data of the ABAP cds view will be displayed like below
Choose any one record and right click on it and choose Follow Association
List of associations will be displayed in a window like below, choose the relevant
association
The below CDS view will result in LEFT OUTER JOIN when executed.
@AbapCatalog.sqlViewName: 'ZV_ORD_HDR'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
on $projection.vbeln = _OrderItems.vbeln
key vbeln,
erdat,
vbtyp,
auart,
netwr,
waerk,
_OrderItems.matnr
}
To achieve INNER JOIN, you need to define the attribute in the path expression
like below.
@AbapCatalog.sqlViewName: 'ZV_ORD_HDR'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
on $projection.vbeln = _OrderItems.vbeln
key vbeln,
erdat,
vbtyp,
auart,
netwr,
waerk,
_OrderItems[inner].matnr