SAP CDS SIMPLE ASSOCIATION V1



Get Example source ABAP code based on a different SAP table
  


• ASSOCIATION ABAP_CDS_SELECT_V1
• $projection ABAP_CDS_SELECT_ASS_V1
• TO ABAP_CDS_SELECT_ASS_V1
• ON ABAP_CDS_SELECT_ASS_V1

ABAP_CDS_DDL - DDIC-Based View, ASSOCIATION

ABAP_SYNTAX
... ASSOCIATION $[ [min..max] $] TO target $[AS _assoc$] ON cds_cond
$[ WITH DEFAULT FILTER cds_cond $] ...

ABAP Addition
1 ... [min..max]
2 ... AS _assoc
3 ... WITH DEFAULT FILTER cds_cond

What does it do?
Defines a CDS association with the name _assoc in a SELECT statement of a ABAP_CDS_V1_VIEW . A CDS association associates the current CDS view as association source with the association target target using an ON condition cds_cond. The association target can be a CDS view entity, a CDS table function, a CDS hierarchy, another CDS custom entity, a CDS abstract entity, a DDIC database table,
BEGIN_SECTION VERSION 5 OUT a DDIC view,
END_SECTION VERSION 5 OUT or a ABAP_CDS_V1_VIEW . If target is a non-SQL CDS entity, the way the CDS association can be used is restricted.
A CDS association of a SELECT statement in a CDS view can be accessed as follows:
By specifying its name in a path expression in the same statement and in all places where this is documented. A CDS association whose association target is a non-SQL CDS entity can only be exposed in the SELECT list.
If a CDS association whose association target is not a CDS abstract entity or a CDS custom entity is exposed using a path expression in the SELECT list of the current SELECT statement, the following can use it in their path expressions:
Other CDS views
AB-SQL statements
Exposed self associations can be specified as hierarchy associations in CDS hierarchies or in the AB_SQL hierarchy generator HIERARCHY.
When a CDS view is activated that uses a path expression to access a CDS association or an element, or when a path expression is used for access in AB_SQL , every CDS association of the path expression is transformed to a join expression. Here, the association source represents the left side and the association target represents the right side. The ON condition of the association is added to the ON condition of the join. By default, the join type is determined by where the path expression is used:
After FROM, it is an inner join inner join (INNER JOIN)
In all other locations, it is a left outer join (LEFT OUTER JOIN)
This setting can be overwritten by specifying the join type when using the CDS association in a path expression. A path expression in a SELECT list that exposes its closing CDS association is not defined as a join. A self association whose association target is the same as the association source cannot be created as a join in the CDS view where it is defined.
When specifying the ON condition, for the operands, general and the following special rules apply:
The fields of the association target must be prefixed in the ON condition by the name of the CDS association (prefix _assoc. separated by a period).
If the CDS association in the SELECT list of the current SELECT statement is exposed, the fields of the association source specified in the ON condition must also be listed in the SELECT list. This ensures that a join expression can be built from the CDS association (when used in a path expression).
To make a reference to an element of the SELECT list in the ON condition, the field name can be prefixed with $projection instead of the association source. In this case, an alternative element name defined using AS can be specified instead of the field name. If a path expression of the SELECT list is used when the prefix $projection is used in the ON condition, the current CDS association cannot itself be used in the SELECT list, to avoid invalid join expressions.



Latest notes:

CDS associations not listed in the SELECT list can only be used in path expressions of the current SELECT statement.
When a CDS view is activated, a join defined by a CDS association is built for every use in a path expression and not for the definition of the CDS association. No joins are constructed for CDS associations that are not used in their CDS views.
If a CDS association is used in a path expression of the SELECT list of the current SELECT statement, a join is defined for the current view. Here, the ON conditions of the CDS association are applied to the join directly. No path expressions are allowed in the ON condition of a join, which means that no reference can be made to a path expression of the SELECT list when using the prefix $projection in the ON condition of a CDS association, if CDS the association is also used here.
CDS associations and join expressions can be used in a SELECT statement of a CDS view. When the join expressions defined by the CDS associations are used in path expressions, all joins are joined.
Special rules apply to CDS associations in SELECT statements joined with UNION.
Cyclical dependencies should be avoided when using CDS associations to prevent problems occurring in mass activations of CDS entities.
CDS associations whose association target is a non-SQL CDS entity cannot be used anywhere where they could produce instances of join expressions.
NON_V5_HINTS
The syntax for defining and using CDS associations is a higher-value wrapping of the syntax for joins. Using CDS associations instead of directly programming joins makes it easier to read the definition of a CDS view. CDS associations can be used to model relationships between CDS entities that can be accessed simply using path expressions in CDS views or in AB_SQL .
ABAP_HINT_END
• [ ] ABAP_CDS_SELECT_ASS_V1

ABAP Addition

What does it do?
Defines the cardinality of the association target of a CDS view, which is defined with a CDS association ASSOCIATION . The square brackets [ ] are part of the syntax. For min and max , positive integers (including 0) and asterisks (*) can be specified:
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 a CDS association is used in a WHERE condition, 1 must be specified for max.
If the cardinality is not defined explicitly, the cardinality to-one is used implicitly ([min..1]).
A cardinality is specified to document the semantics of the data model and, in some database systems, for optimizations. In these database systems, left outer joins (LEFT OUTER JOIN) produced by path expressions are given the addition TO ONE if an explicit or implicit to 1 cardinality is used and the addition TO MANY if any other cardinality is used. These additions work in the same way as when they are specified explicitly in LEFT OUTER JOIN . This means that an optimization is attempted and the result can be undefined if the result set does not match the cardinality.



Latest notes:

To avoid undefined and platform-dependent behavior, the cardinality should always be defined to match the data in question.
The specified cardinality is evaluated by the syntax check for paths specified in the CDS DDL of CDS or in AB_SQL . A non-matching cardinality usually produces a syntax check warning.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Specifies the cardinality of a CDS association incorrectly. The following CDS view joins the DDIC database tables SCARR and SPFLI in a CDS association _spfli without specifying the cardinality explicitly. The implicitly set cardinality is to 1. If the CDS association is used in a path specified in the SELECT list, this is implemented in some database systems (for example the SAP HANA database) as a left outer join using the implicit addition TO ONE . The actual cardinality of the data is, however, TO MANY.
DDLS DEMO_CDS_WRONG_CARDINALITY
The class CL_DEMO_CDS_WRONG_CARDINALITY uses different SELECT statements to access the view. On optimizing database systems, such as the SAP HANA database, the two reads return a different number of rows, potentially an unexpected result.
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
Specifies the cardinality of a CDS association correctly. The following CDS view joins the DDIC database tables SCARR and SPFLI in a CDS association _spfli while specifying the cardinality explicitly. If the CDS association is used in a path specified in the SELECT list, this is implemented in some database systems (for example the SAP HANA database) as a left outer join using the implicit addition TO MANY, which matches the actual cardinality of the data.
DDLS DEMO_CDS_EXPLICIT_CARDINALITY
The class CL_DEMO_CDS_EXPLICIT_CRDNLTY uses different SELECT statements to access the view. The two reads return the same number of rows on all database systems.
ABAP_EXAMPLE_END
• AS ABAP_CDS_SELECT_ASS_V1

ABAP Addition

What does it do?
Defines the name _assoc of a CDS association of a CDS view defined using ASSOCIATION. If no name is defined explicitly using AS, _assoc is set implicitly to the name of the association target. The name _assoc must comply with the naming rules for names.



Latest notes:

It is advisable to use an underscore _ as the first character of the CTE CDS association name.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Example of a simple CDS association. The following CDS view provides the same result as the CDS view DEMO_CDS_SCARR_SPFLI in the joins example, as shown in the class CL_DEMO_CDS_ASSOCIATION using an assertion. Furthermore, the CDS association spfli_scarr is exposed to be used from outside in the SELECT list by specifying a path that contains only the name of a CDS association. The class CL_DEMO_CDS_ASSOCIATION also shows how the CDS association can be accessed by specifying a path in AB_SQL .
DDLS DEMO_CDS_ASSOCIATION
ABAP_EXAMPLE_END
• WITH DEFAULT FILTER ABAP_CDS_SELECT_ASS_V1

ABAP Addition

What does it do?
Defines a standard filter condition for a path expression.
If no filter condition is specified when the CDS association is used in an path expression, the condition cds_cond specified using DEFAULT FILTER is used as the filter condition and applied in an extended condition for the join. The same rules apply to the default filter condition as to a filter condition specified as an attribute.
If a filter condition is specified when the CDS association is used in a path expression, this condition is used instead of the default filter condition.



Latest notes:

When the syntax check evaluates a cardinality specified using [min..max], the default filter condition is respected alongside the ON condition.
NON_V5_HINTS
ABAP_HINT_END