Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Reads →  WITH →  WITH Examples 

WITH, Publishing Associations

This example demonstrates how CDS associations are published using WITH ASSOCIATIONS after a common table expression.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA(out) = cl_demo_output=>new( ).

    WITH
      +cte_2 AS ( SELECT demo_cds_publish_assoc_1~d
                         FROM  demo_cds_publish_assoc_1 )
             WITH ASSOCIATIONS
               ( demo_cds_publish_assoc_1~\_demo_join2 )
      SELECT *
             FROM +cte_2\_demo_join2 AS demo_join2
             INTO TABLE @DATA(result2).
    out->write( result2 ).

    WITH
      +cte_2a AS ( SELECT demo_cds_publish_assoc_1~d
                          FROM  demo_cds_publish_assoc_1
                          WHERE d = 'xx' )
              WITH ASSOCIATIONS
                ( demo_cds_publish_assoc_1~\_demo_join2 )
      SELECT *
             FROM +cte_2a\_demo_join2 AS demo_join2
             INTO TABLE @DATA(result2a).
    out->write( result2a ).

    WITH
      +cte_3 AS ( SELECT demo_cds_publish_assoc_1a~d
                         FROM  demo_cds_publish_assoc_1a )
             WITH ASSOCIATIONS
               ( demo_cds_publish_assoc_1a~\_demo_join2 )
      SELECT *
             FROM +cte_3\_demo_join2 AS demo_join2
             INTO TABLE @DATA(result3).
    out->write( result3 ).

    WITH
      +cte_3a AS ( SELECT demo_cds_publish_assoc_1a~d
                          FROM  demo_cds_publish_assoc_1a
                          WHERE d = 'xx' )
              WITH ASSOCIATIONS
                ( demo_cds_publish_assoc_1a~\_demo_join2 )
      SELECT *
             FROM +cte_3a\_demo_join2 AS demo_join2
             INTO TABLE @DATA(result3a).
    out->write( result3a ).

    out->display( ).

Description

The common table expressions in this example access the CDS views demo_cds_publish_assoc_1 and demo_cds_publish_assoc_1a of the executable example for publishing CDS associations in the SELECT list of CDS views. The subqueries of the common table expressions +cte_2, +cte_2a, +cte_3, and +cte_3a match the queries of the CDS views demo_cds_publish_assoc_2, demo_cds_publish_assoc_2a, demo_cds_publish_assoc_3, and demo_cds_publish_assoc_3a used in the executable example. The additions WITH ASSOCIATIONS publish the CDS association _demo_join2 of the data sources of the subqueries. In the case of the CDS views, this is done in their SELECT list. When accessed using the path expression in the main queries of the WITH statements, the published CDS associations produce the exact same results as the same action in the example for the CDS views. This demonstrates that the results sets of the subqueries are used as the left side of the join instances.