SELECT FROM spfli
INNER JOIN scarr ON
spfli~carrid = scarr~carrid
FIELDS scarr~carrname AS carrier,
spfli~connid AS flight,
spfli~cityfrom AS departure,
spfli~cityto AS arrival
ORDER BY carrier, flight
INTO TABLE @DATA(result_open_sql_join).
@AbapCatalog.sqlViewName: 'DEMO_CDS_JN1'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_join1
as select from spfli
inner join scarr on
spfli.carrid = scarr.carrid
{
scarr.carrname as carrier,
spfli.connid as flight,
spfli.cityfrom as departure,
spfli.cityto as arrival
}
SELECT FROM demo_cds_join1
FIELDS *
ORDER BY carrier, flight
INTO TABLE @DATA(result_cds_join).
Finally, I will use the advanced modelling capabilty of ABAP CDS and transform the explicit join into an association of another view:
@AbapCatalog.sqlViewName: 'DEMO_CDS_JN2'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_join2
as select from spfli
association to scarr as _scarr on
spfli.carrid = _scarr.carrid
{
_scarr[inner].carrname as carrier,
spfli.connid as flight,
spfli.cityfrom as departure,
spfli.cityto as arrival
}
The association _scarr is declared once behind the keyword association and can be used at several places inside the view in path expressions. You can also publish it for usage in other views or in Open SQL, but I have not done that here.
For our simple example, I use the path expression _scarr[inner].carrname as the first element of the select list. When using a path expression, the associations listed there are internally transformed to joins. In the select list those joins are left outer joins by default. Therefore, I override the default with [inner] in order to enforce an inner join. You can check the result by displaying the SQL DDL (shown for HANA here) that is generated from the ABAP CDS DDL in ADT (Context menu Show SQL CREATE statement😞
CREATE VIEW "DEMO_CDS_JN2" AS SELECT
"SPFLI"."MANDT" AS "MANDT",
"=A0"."CARRNAME" AS "CARRIER",
"SPFLI"."CONNID" AS "FLIGHT",
"SPFLI"."CITYFROM" AS "DEPARTURE",
"SPFLI"."CITYTO" AS "ARRIVAL"
FROM "SPFLI" "SPFLI" INNER JOIN "SCARR" "=A0" ON (
"SPFLI"."MANDT" = "=A0"."MANDT" AND
"SPFLI"."CARRID" = "=A0"."CARRID" )
And of course, the data preview of ADT (F8) or the following Open SQL retrieve again the same data as before:
SELECT FROM demo_cds_join2
FIELDS *
ORDER BY carrier, flight
INTO TABLE @DATA(result_cds_assoc).
ASSERT result_cds_join = result_open_sql_join.
ASSERT result_cds_assoc = result_cds_join.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
6 | |
4 | |
4 | |
3 | |
3 | |
2 | |
2 | |
2 |