SAP CDS EXTEND VIEW



Get Example source ABAP code based on a different SAP table
  


• EXTEND VIEW ABAP_CDS_DDL_STATEMENT
• WITH ABAP_CDS_EXTEND_VIEW

ABAP_CDS_DDL - EXTEND VIEW ddic_based

ABAP_SYNTAX
@AbapCatalog.sqlViewAppendName: 'CDS_APPEND_VIEW'
$[@extension_annot1$]
$[@extension_annot2$]
...
EXTEND VIEW ddic_based_view
WITH cds_view_extension
$[association1
association2 ...$]
{ select_list_extension }
$[GROUP BY field1, field2, ... $]
$[UNION $[ALL$] { ... }$] $[;$]

ABAP Addition
1 ... GROUP BY field1, field2, ...
2 ... UNION $[ALL$] { ... }

What does it do?
Extends an existing ABAP_CDS_V1_VIEW ddic_based_view using a CDS view extension cds_view_extension in the CDS DDL. An existing ABAP_CDS_V1_VIEW can have one or more CDS view extensions.
The extended CDS view ddic_based_view must be specified under the name of its CDS entity. The name of the ABAP_CDS_MNG_VIEW cannot be specified.

ABAP_PREREQUISITE
As a prerequisite for the extension of the CDS view with the statement EXTEND VIEW, the elements of the annotation array AbapCatalog.viewEnhancementCategory[ ] must be specified accordingly in its definition with DEFINE VIEW:
By default or if only the value #PROJECTION_LIST is specified, views without aggregate expressions in the SELECT list and without a UNION clause can be extended.
In order to extend views that have aggregate expressions in the SELECT list, the annotation array must contain the value #GROUP_BY alongside #PROJECTION_LIST.
In order to extend views that contain UNION clauses, the annotation array must contain the value #UNION alongside #PROJECTION_LIST.
If the annotation array contains the value #NONE, the view cannot be extended.
ABAP_PREREQ_END

Components of a View Extension
View extensions can make additions to the original view, but it cannot modify, overwrite, or delete elements from the original view. The following components are possible in a CDS view extension:
The annotation AbapCatalog.sqlViewAppendName is mandatory. Further annotations extension_annot1 , extension_annot2 , ... can also be specified. This is optional.
New associations association1, association2 can be defined and exposed in the CDS view extension. The same rules apply that are described in topic CDS DDL - DDIC-based view, ASSOCIATION. ABAP_NOTE It is not allowed to define new CDS compositions or CDS to-parent associations in a view extension.
The elements of the extension list select_list_extension specified after EXTEND VIEW are added to the existing SELECT list. At least one element must be added. It is possible to access all fields of all data sources of the extended view in the extension list select_list_extension. The list can have all elements of a SELECT list, namely:
field of a data source
Input parameters of the extended CDS view. It is not possible to specify new input parameters in a view extension.
It can expose a CDS association of the extended view or a newly defined association
Using a path expressions, a field of an association target can be included. Path expressions are possible for associations of the extended view or for newly defined associations.
literals, session variables, expressions and functions
ABAP_NOTE To add aggregate expressions to a view extension, special rules apply. See below.
Aggregate expressions are possible in the SELECT list of a view extension only if the following conditions are met:
The original view must contain aggregate expressions. If the original view does not contain any aggregate expressions, this is not possible.
The extended view must be annotated with AbapCatalog.viewEnhancementCategory [#PROJECTION_LIST, #GROUP_BY]
If non-aggregated elements are added to a CDS view with aggregate expressions, then the GROUP-BY clause must be extended using the addition GROUP BY
If the original view contains UNION, equivalent UNION additions must be used in the statement EXTEND VIEW. As a prerequisite, the extended view must be annotated with AbapCatalog.viewEnhancementCategory [#PROJECTION_LIST, #UNION] If an appended element already occurs in the existing SELECT list or if a different extension occurs, it must be given an alternative element name using AS.

ABAP_RESTRICTIONS
CDS views with an explicit name list cannot currently be extended.
CDS view extensions themselves cannot be extended.
It is not possible to specify new input parameters in a view extension.
No addition ROOT can be specified in the statement EXTEND VIEW to transform a regular view to a root entity.
An appended field cannot be defined as a key field using KEY.
It is not allowed to define new to-parent or composition associations in a view extension.
For views that have an element of data type LRAW or LCHR in the SELECT list, no view extension is possible and the annotation AbapCatalog.viewEnhancementCategory[#NONE] is enforced. The reason is that elements of data types LRAW or LCHR must always be in the last position of the SELECT list.
ABAP_RESTR_END

Naming
Two repository objects are created for a CDS view extension that is defined using EXTEND VIEW. A name must be specified for each of the two objects:
The actual name cds_view_extension of the CDS view extension is specified after the keywords EXTEND VIEW. This name follows the same rules as the name of an DDIC append view, but can have up to 30 characters.
The name CDS_APPEND_VIEW for an append view in ABAP Dictionary must be specified in quotation marks after the annotation @AbapCatalog.sqlViewAppendName. This name is subject to the same rules as the name of a DDIC database view in ABAP Dictionary, see topic TITLE . The new DDIC append view extends the ABAP_CDS_MNG_VIEW of the extended CDS view. The name given to the DDIC append view can no longer be changed after the CDS view extension is transported into a follow-on system.
The name of the new DDIC append view and of the actual CDS view extension should be located in the customer namespace (or in the namespace of a partner or special development) to protect it against being overwritten by upgrades or new releases.
Once the DDL source code of a CDS view extension has been transported, the actual name cds_view_extension and the name of the DDIC append view CDS_APPEND_VIEW can no longer be changed. The extended view ddic_based_view can also no longer be changed.
ABAP_NOTE The DDL source code of a CDS view extension does not need to have the same name as the CDS view extension entity, but it is advisable to use the name of the entity.



Latest notes:

Currently it is not possible to define extension categories for CDS views. As a consequence, the following restrictions apply:
CDS views have the property can be extended in any way with respect to the extension category of DDIC structures. The consequences of this must be respected when extending a CDS view.
The properties of a CDS view defined using annotations, such as switching on table buffering, cannot currently be modified using extensions.
CDS view extensions specified with EXTEND ENTITY are recommended for ABAP_CDS_V1_VIEWS only. However, for compatibility and migration reasons, it is also possible to extend CDS projection views or CDS view entities with the statement EXTEND VIEW . However, view extends defined using EXTEND VIEW do not support the syntax elements specific to projection views and CDS view entities. Therefore, it is recommended that the statement EXTEND VIEW ENTITY is used for extensions of CDS projection views and CDS view entities instead.
If, for some reason, a CDS projection view or a CDS view entity is extended with EXTEND VIEW, the ABAP_CDS_MNG_VIEW is deleted (as CDS projection views and CDS view entities do not have any DDIC artefacts), and entity annotations referring to the CDS-managed DDIC view are ignored.
ABAP_CDS_V1_VIEWS are obsolete. If new CDS views are required, ABAP_CDS_V2_VIEWS should be used.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following CDS view extension
DDLS DEMO_CDS_VIEW_EXTENSION
adds two view fields to the existing CDS view.
DDLS DEMO_CDS_ORIGINAL_VIEW
The DDIC append view DEMO_CDS_EXTENS is created in ABAP Dictionary.
BEGIN_SECTION VERSION 5 OUT The class CL_DEMO_CDS_VIEW_EXTENSION uses the statement SELECT to access the extended view and also displays the components of the dictionary structures in question.
END_SECTION VERSION 5 OUT
ABAP_EXAMPLE_END
• GROUP BY ABAP_CDS_EXTEND_VIEW

ABAP Addition

What does it do?
This addition must be specified if elements not defined using aggregate expressions are added to a view with aggregate expressions. These elements must be specified after GROUP BY add extend the GROUP-BY clause of the original view. With respect to the extended view, the extended GROUP-BY clause must follow the general rules for a GROUP-BY clause.
The addition cannot be specified if the definition of the original view does not contain any aggregate expressions in its SELECT list.



Latest notes:

Extensions of a view with aggregate expressions require it to contain the annotation array viewEnhancementCategory[ ] with the value #GROUP_BY.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following CDS view extension
DDLS DEMO_CDS_EXTEND_AGGREGATE
extends the existing CDS view.
DDLS DEMO_CDS_AGGREGATE
A database field connid and an aggregate expression sum(distance) are added to the SELECT list. Accordingly, the addition GROUP BY must be used to add the database field to the GROUP-BY clause of the original view.
ABAP_EXAMPLE_END
• ALL ABAP_CDS_EXTEND_VIEW
• UNION ABAP_CDS_EXTEND_VIEW

ABAP Addition

What does it do?
This addition must be specified when a view with UNION clauses is extended. A corresponding UNION addition must be specified for each UNION clause of the original view. The addition ALL must be specified each time it is specified in the associated UNION clause of the original view. The curly brackets can contain elements that extend the SELECT list of the associated UNION clause of the original view. As specified by the SELECT list extended using select_list_extension, the UNION clauses must be extended so that the rules for UNION clauses are not broken in the extended view.
The addition cannot be specified if the definition of the original view does not have a UNION clause.



Latest notes:

Extensions of a view with UNION clauses require it to contain the annotation array viewEnhancementCategory[ ] with the value #UNION.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following CDS view extension
DDLS DEMO_CDS_EXTEND_UNION
extends the existing CDS view.
DDLS DEMO_CDS_UNION
The original view has two UNION clauses represented using corresponding UNION additions in the definition of the CDS view extension. Two elements with matching types are added to the three SELECT lists of the original view.
ABAP_EXAMPLE_END