SAP CDS EXTEND VIEW ENTITY



Get Example source ABAP code based on a different SAP table
  


• EXTEND VIEW ENTITY ABAP_CDS_DDL_STATEMENT
• WITH ABAP_CDS_EXT_VIEW_ENT

ABAP_CDS_DDL - EXTEND VIEW ENTITY

ABAP_SYNTAX

EXTEND VIEW ENTITY cds_view_entity $| projection_view
WITH
$[association1
association2 ...$]
{ select_list_extension }
$[GROUP BY field1, field2, ... $]
$[EXCEPT $| INTERSECT $| UNION $[ALL$] { ... }$] $[;$]

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

What does it do?
Extends an existing CDS view entity cds_view_entity or an existing CDS projection view projection_view using a CDS view entity extension in the CDS DDL. An existing CDS view can have one or more CDS view entity extensions.

ABAP_PREREQUISITE
As a prerequisite for the extension of the CDS entity with the statement EXTEND VIEW ENTITY, the elements of the annotation array AbapCatalog.viewEnhancementCategory[ ] must be specified accordingly in its definition with DEFINE VIEW ENTITY:
By default or if only the value #PROJECTION_LIST is specified, views without aggregate expressions in the SELECT list and without a set-operator 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 set-operator clauses, the annotation array must contain the value #UNION alongside #PROJECTION_LIST. ABAP_NOTE The annotation array value #UNION allows extensions not only for union views, but for views including any set operator (also EXCEPT and INTERSECT). The annotation array value #UNION is a known misnomer.
If the annotation array contains the value #NONE, the view cannot be extended.
Additionally, the annotation AbapCatalog.extensibility.extensible must not be set to false.
ABAP_PREREQ_END

Components of a View Entity Extension
View entity extensions can make additions to the original view, but they cannot modify, overwrite, or delete elements from the original view. The following components are possible in a CDS view entity extension:
New associations association1, association2 can be defined and exposed in the CDS view entity extension. The same rules apply that are described in topic CDS DDL - CDS view entity, ASSOCIATION. ABAP_NOTE It is not allowed to define new CDS to-parent associations in a view entity extension. New CDS composition associations are allowed under the condition that the composition target is also defined as composition child and has the required to-parent association.
When extending a CDS projection view, the view entity extension can specify all features that are possible in a projection view, such as CDS virtual elements, localized elements, and redefined and redirected associations. See topic TITLE for a complete list.
When extending a CDS view entity, the view entity extension can specify all features that are possible in a view entity, such as path expressions, elementary operands, expressions and functions. See topic TITLE for a complete list.
Each appended element must have a unique name. If an appended element already occurs in the existing SELECT list or in a different extension, the element must be given an alternative element name using AS.

ABAP_RESTRICTIONS
CDS view entity extensions themselves cannot be extended.
It is not possible to specify new input parameters in a view entity extension.
No addition ROOT can be specified in the statement EXTEND VIEW ENTITY to transform a regular view to a root entity.
An appended field cannot be defined as a KEY field.
In a view entity extension, header annotations are not supported. That means that no annotations are allowed in front of the statement EXTEND VIEW ENTITY.
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.
It is not possible enable both buffering and extensions for a view entity. If the annotation @AbapCatalog.entityBuffer.definitionAllowed is set to true , extensions are not allowed.
CDS view entity extensions cannot append fields of CDS projection views, CDS custom entities, or CDS abstract entities to a CDS view entity. Fields of CDS projection views, CDS abstract entities, or CDS custom entities can - generally - not be used in path expressions and this also holds true for path expressions in CDS view entity extensions. This is not valid: extend view entity MyExtension
with {
_assocToAbstract.field1 as Alias1
_assocToCustom.field2 as Alias2
_assocToProjection.field3 as Alias3
}
ABAP_RESTR_END
VX_EXA_ONLY

ABAP_EXAMPLE_VX
The following CDS view entity extension
DDLS DEMO_CDS_VIEW_ENTITY_EXTENSION
adds two view fields to the existing CDS view entity
DDLS DEMO_CDS_ORIGINAL_VIEW_VE
The class CL_DEMO_CDS_VIEW_EXTENSION_VE uses the statement SELECT to access the extended view entity.
ABAP_EXAMPLE_END
• GROUP BY ABAP_CDS_EXT_VIEW_ENT

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 and they 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.
As a prerequisite, the definition of the original view must contain at least one aggregate expression in its SELECT list.



Latest notes:

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



Example ABAP Coding

The following CDS view entity extension
DDLS DEMO_CDS_EXTEND_GROUP_BY
extends the existing CDS view entity
DDLS DEMO_CDS_VIEW_ENTITY_GROUP_BY
A database field connid and an aggregate expression sum(sflight.paymentsum) 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_EXT_VIEW_ENT
• UNION ABAP_CDS_EXT_VIEW_ENT

ABAP Addition

What does it do?
This addition must be specified when a view entity with a set operator ( EXCEPT, INTERSECT, or UNION) is extended. A corresponding addition (EXCEPT , INTERSECT, or UNION) must be specified for each clause of the original view. The curly brackets can contain elements that extend the SELECT list of the associated set-operator clause of the original view. As specified by the SELECT list extended using select_list_extension, the set-operator clauses must be extended so that the rules for set-operator clauses are not broken in the extended view.
The addition ALL is available only for UNION views and it must be specified each time it is specified in the associated UNION clause of the original view.
As a prerequisite, the original view must have a set-operator clause.



Latest notes:

Extensions of a view with set-operator clauses require it to contain the annotation array viewEnhancementCategory[ ] with the values #PROJECTION_LIST and #UNION.
NON_V5_HINTS
ABAP_HINT_END
VX_EXA_ONLY

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