SAP WITH ASSOCIATIONS ABAP Statements



Get Example source ABAP code based on a different SAP table
  


• AS WITH ASSOCIATIONS
• WITH ASSOCIATIONS ( ... ) WITH

WITH, ASSOCIATIONS

ABAP_SYNTAX
... WITH ASSOCIATIONS ( path
$|join$[,
...$] )
$|(assoc_syntax)

What does it do?
The addition WITH ASSOCIATIONS for the definition of a common table expression exposes associations of this table expression for use in the subsequent queries of a WITH statement. The following can be specified:
Existing CDS associations or CTE associations of the common table expression using SQL path expressions path.
Definitions of new CTE associations of the common table expression using join.
The parentheses must contain at least one association and multiple associations can be specified as a comma-separated list. Both existing and new associations can be specified together and in any order.
In subsequent queries of the current WITH statement, the exposed associations can be used as root elements of the path expressions in question.
In the specified columns of SELECT statements of the subsequent queries.
As a data source of the FROM clause of the subsequent queries.
Exposed self-associations can be specified in the hierarchy generator HIERARCHY as a hierarchy association.
An exposed association can be exposed again by a subsequent common table expression of the current WITH statement using the addition WITH ASSOCIATIONS, as long as this addition uses the exposing table expression as a data source. Different attributes can be specified here.
Instead of specifying associations statically in parentheses, a parenthesized data object assoc_syntax can be specified. When the statement is executed, the data object must contain the syntax shown for the static specification. In this case, the common table expression can be used only in other dynamic tokens of the WITH statement. The data object assoc_syntax can be a character-like data object or a standard table with a character-like row type. The syntax in assoc_syntax, as in the static syntax, is not case-sensitive. When an internal table is specified, the syntax can be distributed across multiple rows. Invalid syntax raises a catchable exception from the class CX_SY_DYNAMIC_OSQL_ERROR.
If an association is exposed more than once under different alias names, it is handled as a separate association when used in a path expression. A separate instance of a join expression is created for each association used and each expression uses the result set of the exposing common table expression as its left side.
The addition WITH ASSOCIATIONS cannot be used if the subquery of the common table expression contains the set operator UNION, INTERSECT, or EXCEPT. Furthermore, it is not possible to use the addition USING
BEGIN_SECTION VERSION 5 OUT or the obsolete addition CLIENT SPECIFIED
END_SECTION VERSION 5 OUT in the current CTE, nor in CTEs where the addition REDIRECTED TO is applied, nor in queries that use the association in a path expression and that transform it to a join expression.



Latest notes:

When an association of the data source of a common table expression is exposed using WITH ASSOCIATION, this is the same as exposing an association of the data source of a CDS view entity in its SELECT list.
It should be noted that the exposing common table expression itself is the association source of the exposed association and not the data source of the expression. The exposing common table expression replaces the original association source of the association. The left side of a join expression created for the exposed association is the result set of the subquery of the common table expression.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following example class CL_DEMO_WITH_ASSOCIATIONS demonstrates how CDS associations are exposed by common table expressions. It works in the same way as the executable example for using path expressions in the FROM clause. The main query uses the alias name _spfli_scarr to access the association target SCARR of the final CDS association _scarr of the path expression _spfli[ fltime > @fltime ] _scarr exposed for the common table expression +cte. The SELECT list of the common table expression must specify the component _spfli-carrid with the same filter condition fltime > @fltime as in the exposed association so that the associated join can be created.
ABEXA 00769
ABAP_EXAMPLE_END

Return to menu