Ansicht
Dokumentation

ABENCDS_SIMPLE_ASSOCIATION_V2 - CDS SIMPLE ASSOCIATION V2

ABENCDS_SIMPLE_ASSOCIATION_V2 - CDS SIMPLE ASSOCIATION V2

Addresses (Business Address Services)   CL_GUI_FRONTEND_SERVICES - Frontend Services  
This documentation is copyright by SAP AG.
SAP E-Book

- CDS View Entity, ASSOCIATION

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


Additions

1. ... [min..max]

2. ... AS _assoc

3. ... ON cds_cond

4. ... WITH DEFAULT FILTER cds_cond

Effect

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 , 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 CDS DDL - path_expr, join_type.

Limitations

  • 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.

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.
  • 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.

Example

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 [1..1].
  • 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 [1..*].

The CDS view entity can be accessed in an ABAP program with a simple 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.

Addition 1

... [min..max]

Effect

Defines the cardinality of the association target of a CDS view entity, 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.

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 . A non-matching cardinality usually produces a syntax check warning.

Example

Specifies the cardinality of a CDS association incorrectly. The following CDS view entity 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.

The program DEMO_CDS_WRONG_CARDINALITY_1 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.

Example

Specifies the cardinality of a CDS association correctly. The following CDS view entity 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.

The program DEMO_CDS_EXPLICIT_CARD_1 uses different SELECT statements to access the view. The two reads return the same number of rows on all database systems.

Addition 2

... AS _assoc

Effect

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.

Note

It is recommended that an underscore _ is used as the first character of the CDS association name.

Addition 3

... ON cds_cond

Effect

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.
typed literals are currently not supported in this position.
typed literals are currently not supported in this position.
  • Path expressions are not allowed.
  • CDS DDL expressions are not allowed.

Addition 4

... WITH DEFAULT FILTER cds_cond

Effect

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.

Note

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






ABAP Short Reference   TXBHW - Original Tax Base Amount in Local Currency  
This documentation is copyright by SAP AG.

Length: 25894 Date: 20240329 Time: 133352     sap01-206 ( 310 ms )