Skip to content

ABAP Keyword Documentation →  ABAP - Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Data Definitions →  ABAP CDS - DDL for Data Definitions →  ABAP CDS - DEFINE VIEW →  ABAP CDS - SELECT →  ABAP CDS - SELECT, ASSOCIATION →  ABAP CDS - path_expr 

ABAP CDS - Joins of Associations

This example demonstrates how to implement path expressions in joins in ABAP CDS.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA:
      path_outer TYPE TABLE OF demo_cds_assoc_join1_o WITH DEFAULT KEY,
      path_inner TYPE TABLE OF demo_cds_assoc_join1_i WITH DEFAULT KEY,
      join_outer TYPE TABLE OF wa WITH DEFAULT KEY,
      join_inner TYPE TABLE OF wa WITH DEFAULT KEY.
    out->begin_section( 'CDS Views'
      )->begin_section( 'Path with [left outer]' ).
    SELECT *
           FROM demo_cds_assoc_join1_o
           INTO TABLE @path_outer.
    SORT path_outer.
    out->write( path_outer
      )->next_section( 'Path with [inner]' ).
    SELECT *
           FROM demo_cds_assoc_join1_i
           INTO TABLE @path_inner.
    SORT path_inner.
    out->write( path_inner
      )->end_section( )->end_section(
      )->begin_section( `Open SQL Joins`
      )->begin_section(
        `LEFT OUTER JOINs` ).
    SELECT FROM
             demo_cds_assoc_join1_o AS t1
               LEFT OUTER JOIN
                 demo_cds_assoc_join2 AS t2 ON t2~d = t1~d_1
                     LEFT OUTER JOIN
                       demo_join3 AS t3 ON t3~l = t2~d
           FIELDS t1~a_1,
                  t1~b_1,
                  t1~c_1,
                  t1~d_1,
                  t2~d AS d_2,
                  t2~e AS e_2,
                  t2~f AS f_2,
                  t2~g AS g_2,
                  t2~h AS h_2,
                  t3~i AS i_3,
                  t3~j AS j_3,
                  t3~k AS k_3,
                  t3~l AS l_3
           INTO CORRESPONDING FIELDS OF TABLE @join_outer.
    SORT join_outer.
    out->write( join_outer
      )->next_section( `INNER JOINs` ).
    SELECT FROM
             demo_cds_assoc_join1_i AS t1
               INNER JOIN
                 demo_cds_assoc_join2 AS t2 ON t2~d = t1~d_1
                     INNER JOIN
                       demo_join3 AS t3 ON t3~l = t2~d
           FIELDS t1~a_1,
                  t1~b_1,
                  t1~c_1,
                  t1~d_1,
                  t2~d AS d_2,
                  t2~e AS e_2,
                  t2~f AS f_2,
                  t2~g AS g_2,
                  t2~h AS h_2,
                  t3~i AS i_3,
                  t3~j AS j_3,
                  t3~k AS k_3,
                  t3~l AS l_3
           INTO CORRESPONDING FIELDS OF TABLE @join_inner.
    SORT join_inner.
    out->write( join_inner )->display( ).
    ASSERT path_inner = join_inner.
    ASSERT path_outer = join_outer.

Description

This example demonstrates how joins are used for path expressions with CDS associations. The following two CDS views - DEMO_CDS_ASSOC_JOIN1_O and DEMO_CDS_ASSOC_JOIN1_I - access their own association _demo_join2 in path expressions of the SELECT list and the published CDS association _demo_join3 of DEMO_CDS_ASSOC_JOIN2.

@AbapCatalog.sqlViewName: 'DEMO_CDS_ASJO1O'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view Demo_Cds_Assoc_Join1_o
  as select from demo_join1
  association to Demo_Cds_Assoc_Join2 as _demo_join2 on
    _demo_join2.d = demo_join1.d
{
  demo_join1.a                                      as a_1,
  demo_join1.b                                      as b_1,
  demo_join1.c                                      as c_1,
  demo_join1.d                                      as d_1,
  _demo_join2[left outer].d                         as d_2,
  _demo_join2[left outer].e                         as e_2,
  _demo_join2[left outer].f                         as f_2,
  _demo_join2[left outer].g                         as g_2,
  _demo_join2[left outer].h                         as h_2,
  _demo_join2[left outer]._demo_join3[left outer].i as i_3,
  _demo_join2[left outer]._demo_join3[left outer].j as j_3,
  _demo_join2[left outer]._demo_join3[left outer].k as k_3,
  _demo_join2[left outer]._demo_join3[left outer].l as l_3
}  
  
@AbapCatalog.sqlViewName: 'DEMO_CDS_ASJO1I'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view Demo_Cds_Assoc_Join1_i
  as select from demo_join1
  association to Demo_Cds_Assoc_Join2 as _demo_join2 on
    _demo_join2.d = demo_join1.d
{
  demo_join1.a                            as a_1,
  demo_join1.b                            as b_1,
  demo_join1.c                            as c_1,
  demo_join1.d                            as d_1,
  _demo_join2[inner].d                    as d_2,
  _demo_join2[inner].e                    as e_2,
  _demo_join2[inner].f                    as f_2,
  _demo_join2[inner].g                    as g_2,
  _demo_join2[inner].h                    as h_2,
  _demo_join2[inner]._demo_join3[inner].i as i_3,
  _demo_join2[inner]._demo_join3[inner].j as j_3,
  _demo_join2[inner]._demo_join3[inner].k as k_3,
  _demo_join2[inner]._demo_join3[inner].l as l_3
}  
  
@AbapCatalog.sqlViewName: 'DEMO_CDS_ASJO2'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view Demo_Cds_Assoc_Join2
  as select from demo_join2
  association [*] to demo_join3 as _demo_join3 on
    _demo_join3.l = demo_join2.d
{
  _demo_join3,
  demo_join2.d,
  demo_join2.e,
  demo_join2.f,
  demo_join2.g,
  demo_join2.h
}  
  

LEFT OUTER and INNER are explicitly specified in the path expressions.

  • LEFT OUTER affects the implementation of CDS associations in a left outer join, which is the default behavior at these places.
  • INNER affects the implementation of CDS associations in an inner join, which overwrites the default behavior at these places.

In the DDL Source Text Editor of the ADT, you can view the relevant configuration of the join in the display of the generated SQL DDL statement.

This example shows that the result sets of the CDS views DEMO_CDS_ASSOC_JOIN1_O and DEMO_CDS_ASSOC_JOIN1_I are identical to the result sets of ABAP SQL statements SELECT with explicitly programmed left outer and inner joins. The CDS views and the database table DEMO_JOIN3 above are accessed. All involved database tables are filled with sample data. With an inner join, unlike a left outer join, only the row is returned for which the join condition t3~l = t2~d is fulfilled.