SAP CDS SIMPLE ASSOCIATION V2



Get Example source ABAP code based on a different SAP table
  


• ASSOCIATION ABAP_CDS_SELECT
• $projection ABAP_CDS_SELECT_ASSO
• TO ABAP_CDS_SELECT_ASSO
• ON ABAP_CDS_SELECT_ASSO

ABAP_CDS_DDL - CDS View Entity, ASSOCIATION

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

ABAP Addition
1 ... cardinality
2 ... AS _assoc
3 ... ON cds_cond
4 ... WITH DEFAULT FILTER cds_cond

What does it do?
Defines a CDS association with the name _assoc in a SELECT statement of a CDS view entity. A CDS association associates the current CDS view entity as association source with the association target target using an ON condition cds_cond. The association target target can be a DDIC database table, another CDS view entity, a CDS projection view, a ABAP_CDS_V1_VIEW , a table function, a hierarchy, an abstract entity, or a custom entity. If target is a non-SQL CDS entity, the way the CDS association can be used is restricted.
Usage of associations in the SELECT list: All use cases of a CDS association are described in topic CDS DDL - CDS associations and joins .
Associations and joins: in some cases, a CDS association is internally transformed into a join. All use cases of CDS associations and information about the joins created on the database are described in topic CDS DDL - CDS associations and joins .
The automatically generated join type can be changed by specifying a different join type in the attributes of a path expression, see topic TITLE .

Limitations
CDS associations whose association target is a non-SQL CDS entity cannot be used anywhere where they could produce instances of join expressions.
CDS associations whose association target is a CDS projection view cannot be used in path expressions. It is also not possible to add a field from the association target to the element list.



Latest notes:

CDS associations and join expressions can both be used in a SELECT statement of a CDS view entity. A path expression can even consist of a mix of joins and associations. When accessed, the associations and joins are merged.
Special rules apply to CDS associations in SELECT statements with set operators.
Cyclical dependencies should be avoided when using CDS associations to prevent problems occurring in mass activations of CDS entities.
Exposed self associations can be specified as hierarchy associations in CDS hierarchies or in the AB_SQL hierarchy generator HIERARCHY.
A self association whose association target is the same as the association source cannot be created as a join in the CDS view entity where it is defined. So it is not allowed to use a self association in a position where a join is generated.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The CDS view entity DEMO_SALES_CDS_INV_HEADER shown below returns information about sales invoices and works with the DDIC database tables DEMO_SO_INV_HE , DEMO_SALES_ORDER, DEMO_SALES_BUPA, and DEMO_SALES_SO_I.
Two CDS associations are defined:
_buyer stands for a join between the current view and the association target DEMO_SALES_BUPA.
_invoice_items stands for a join between the current view and the association target DEMO_SALES_SO_I.
The association source fields used in the ON conditions, buyer_id and so_key, are part of the SELECT list.
The CDS association _buyer is not exposed in the SELECT list and can only be used in path expressions of the current SELECT statement. This association can be specified in the WHERE condition due to the cardinality to-one.
The CDS association _invoice_items is not accessed in path expressions of the current SELECT statement. However, this association is exposed in the SELECT list, which means it can be used in path expressions of other CDS views. This association cannot be specified in a WHERE condition due to the cardinality to-many.
DDLS DEMO_SALES_CDS_INV_HEADER
The CDS view entity can be accessed in an ABAP program with a simple AB_SQL SELECT statement. SELECT id, birth_name, family_name
FROM demo_sales_cds_inv_header
INTO TABLE @DATA(itab).
The complexity of the actual query is wrapped transparently in the CDS view entity for the application programmer. When the view is accessed, the join (defined by the CDS association _invoice_items) between DEMO_SO_INV_HE and DEMO_SALES_SO_I is not built, because there are no path expressions that need to access the join.
The CDS view entity DEMO_SALES_CDS_INV_HEADER shown above is used as the data source in the definition of the CDS view entity DEMO_SALES_CDS_SO_ITEM. This data source is used to access the exposed CDS association _invoice_items. The elements of the CDS association are accessed in this view. There is no visual indication that it is the result of a join. This join between DEMO_SALES_CDS_INV_HEADER and DEMO_SALES_SO_I is created when the CDS view entity DEMO_SALES_CDS_SO_ITEM is activated. The other CDS association _buyer of the CDS view entity DEMO_SALES_CDS_INV_HEADER cannot be accessed.
DDLS DEMO_SALES_CDS_SO_ITEM
ABAP_EXAMPLE_END

ABAP Addition

What does it do?
A cardinality to express the relation between the rows of the data sources can optionally be specified. The following options are available:
The cardinality can be specified in words, for example: association of many to many AssocTarget as Name on CdsCond Note that the syntax addition TO is left out in this case. This is the recommended option.
The cardinality of the association target can be specified in numbers in square brackets, for example: association[0..1] to AssocTarget as Name on CdsCond Note that the association target is specified after TO in this case.
No cardinality can be specified, as in the following example: association to AssocTarget as Name on CdsCond Note that the association target is specified after TO in this case. If the cardinality is not defined explicitly, the cardinality many-to-one is used implicitly.
For further details, see topic TITLE .
• AS ABAP_CDS_SELECT_ASSO

ABAP Addition

What does it do?
Defines the name _assoc of a CDS 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 recommended that an underscore _ is used as the first character of the CDS association name.
NON_V5_HINTS
ABAP_HINT_END

ABAP Addition

What does it do?
When specifying the ON condition, the following rules apply:
Fields of the association source must be prefixed by the name of the data source. If an alias name was assigned, then the alias name must be used as prefix.
The fields of the association target must be prefixed by the name of the CDS association (prefix _assoc. separated by a period).
Fields that are listed in the SELECT list of the current view entity must be prefixed with $projection.
If the prefix $projection refers to a path expression, a literal, or an expression of the SELECT list, then the current CDS association cannot be exposed in the SELECT list to avoid invalid join expressions.
If the CDS association is exposed in the SELECT list of the current SELECT statement, 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).
The following rules apply to the operands and syntax of the ON condition:
All relational operators are allowed. That means all comparison operators are allowed as well as BETWEEN , LIKE, IS [NOT] NULL , and IS [NOT] INITIAL.
The Boolean operators NOT, AND, and OR are allowed.
lhs can be a field of one of the two data sources data_source of the CDS association. If a comparison operator is used as operator, then lhs can also be an untyped literal. ABAP_NOTE typed literals are currently not supported in this position.
rhs can be a field of both data sources data_source of the CDS association, an untyped literal, a parameter, or a session variable. ABAP_EXCEPTION if the operator LIKE is used, then rhs must be a character literal. ABAP_NOTE typed literals are currently not supported in this position.
Path expressions are not allowed.
CDS DDL expressions are not allowed.
• WITH DEFAULT FILTER ABAP_CDS_SELECT_ASSO

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 a 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