SAP CDS DEFINE VIEW AS PROJECTION



Get Example source ABAP code based on a different SAP table
  


• DEFINE VIEW ENTITY AS PROJECTION ON ABAP_CDS_TRANS_QUERY

ABAP_CDS_DDL - DEFINE VIEW ENTITY AS PROJECTION ON,Transactional Query

ABAP_SYNTAX
$[@entity_annot1$]
$[@entity_annot2$]
...
$[@proj_view_annot1$]
$[@proj_view_annot2$]
...
DEFINE $[ROOT$] VIEW ENTITY projection_view
$[PROVIDER CONTRACT TRANSACTIONAL_QUERY$]
AS PROJECTION ON cds_entity $[AS alias_name$]
$[association1 association2 ...$]
$[redefined_assoc1 redefined_assoc2 ...$]
{ projection_list }
$[WHERE cds_cond$]

ABAP Addition
1 ... ROOT
2 ... AS alias_name

What does it do?
Defines a CDS transactional query with the name projection_view in CDS DDL . A CDS projection view is a direct projection of an underlying CDS entity cds_entity (called projected entity) and exposes a subset of the elements of the projected entity, which are defined in the projection list projection_list.
The projected entity cds_entity can be either a CDS view entity, a CDS transactional interface, or a ABAP_CDS_V1_VIEW . No other CDS entities or data sources are allowed as projected entity. Path expressions are not allowed for specifying the projected entity. One CDS entity can be used as basis for multiple CDS projection views.
The name of the CDS projection view projection_view is defined after the DEFINE VIEW ENTITY statement. The usual rules for ABAP Dictionary views apply to this name and it is not case-sensitive (it is transformed internally into uppercase letters). It can have a maximum of 30 characters and is in the namespace of all global types of an AS ABAP. The name of the DDL source and of the CDS entity must be identical.

ABAP_PREREQUISITES
A CDS transactional query that uses the addition PROVIDER CONTRACT TRANSACTIONAL_QUERY must be part of a RAP business object. That means that the view must either be a root entity itself, or it must be part of a composition tree and reach a root entity via a composition.
ABAP_PREREQ_END

Components
Header annotations specified before the statement DEFINE VIEW ENTITY are not inherited from the projected entity. It is possible, but not mandatory, to specify entity annotations @entity_annot or projection view annotations @proj_view_annot as header annotations.
The provider contract specifies the scenario in which the projection view is used. It is recommended that a provider contract is always specified. Otherwise, no runtime-specific syntax checks are applied. See the topic about provider contracts for further details.
It is possible to add new, local, read-only associations association1, association2 to a projection view after the view definition with DEFINE VIEW ENTITY AS PROJECTION ON. The same rules apply as to associations in CDS view entities. The main difference is that read-only associations in projection views can only be exposed but they cannot be used in path expressions to expose elements from new associations. Details on new local associations in CDS projection views can be found in topic CDS DDL - projection_view, ASSOCIATION.
The projection list projection_list can consist of elements of the projected entity, path expressions, exposed associations, and virtual elements.
Optionally, the number of rows included in the result set can be further restricted by the addition WHERE.



Latest notes:

CDS access control defined for a CDS transactional query must be defined symmetrically to the CDS access control for the projected entity. All behavior of the access control of the projected entity must be inherited.
For an overview of all possible data sources of all available CDS entities, see Data Source Matrix.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following CDS transactional query DEMO_SALES_PV_BUPA is a projection of the existing CDS view DEMO_SALES_CDS_BUPA. The result set contains only rows where the last name of the business partner starts with P.
DDLS DEMO_SALES_PV_BUPA
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
The following CDS transactional query DEMO_SALES_PV_SO_I_SL is a projection of the existing CDS view DEMO_SALES_CDS_SO_I_SL. It exposes a subset of the elements of the projected entity as well as an association.
A provider contract is not specified, since the provider contract is specified only once in the root entity of a CDS composition tree and all child entities automatically inherit this provider contract. In this case, the root entity is DEMO_SALES_PV_SO and it specifies the business object as transactional query.
DDLS DEMO_SALES_PV_SO_I_SL
ABAP_EXAMPLE_END
• ROOT ABAP_CDS_TRANS_QUERY

ABAP Addition

What does it do?
The keyword ROOT specifies that the projection view is the root entity of a composition tree. The projection view must always reflect the position of the projected entity in the composition tree: if the projected entity is a root entity , the projection view must be a root as well. If the projected entity is not a root, the projection view cannot be a root. In this case, the projected entity must contain a to-parent association.
ABAP_NOTE If a CDS projection view is defined as transactional query using the addition PROVIDER CONTRACT TRANSACTIONAL_QUERY, then the view must either be a root entity itself, or it must be part of a composition tree and reach a root entity via an association.
• AS ABAP_CDS_TRANS_QUERY

ABAP Addition

What does it do?
Defines the name alias_name as alias name of the projected entity. The alias name can have a maximum of 30 characters. If no name is defined explicitly using AS, then the original name of the data source is used.