ABAP - Keyword Documentation →  ABAP - Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Data Definitions →  ABAP CDS - DDL for Data Definitions → 

ABAP CDS - EXTEND VIEW

Syntax

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


Extras:

1. ... GROUP BY field1, field2, ...

2. ... UNION [ALL] { ... }

Effect

Extends an existing CDS view cds_entity using a CDS view extension cds_view_extension in the CDS DDL. The extended CDS view must be specified under the name of its CDS entity. The name of the CDS database view cannot be specified here.

The elements of the annotation array AbapCatalog.viewEnhancementCategory[ ] must be specified accordingly using DEFINE VIEW in its definition before the CDS view cds_entity can be extended using the statement EXTEND VIEW:

CDS views with an explicit name list cannot currently be extended. CDS view extensions themselves cannot be extended.

EXTEND VIEW is used to make the following modification-free extensions:

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. An appended field cannot be defined as a key field using KEY.

If the original view contains aggregate expressions, further aggregate expressions can be added to it in select_list_extension. If the original view does not contain any aggregate expressions, this is not possible. If other elements are added to a CDS view with aggregate expressions, its GROUP-BY clause must be extended accordingly using the addition GROUP BY of the association EXTEND VIEW

If the original view contains UNION, equivalent UNION additions must be used in the statement EXTEND VIEW.

The annotation AbapCatalog.sqlViewAppendName must be specified before the view extension itself is defined using EXTEND VIEW. Further annotations extension_annot1, extension_annot2, ... can also be specified. This is optional.

Two ABAP Dictionary 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:

CDS view extensionsare connected to Switch Framework whenever they are defined in a package that is assigned a switch.

Notes

Example

The following CDS view extension

@AbapCatalog.sqlViewAppendName: 'DEMO_CDS_EXTENS'
extend view demo_cds_original_view with demo_cds_view_extension
{
spfli.distance,
spfli.distid as unit
};    

adds two view fields to the existing CDS view.

@AbapCatalog.sqlViewName: 'DEMO_CDS_ORIG'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST]
define view demo_cds_original_view
as select from
spfli
join scarr on
scarr.carrid = spfli.carrid
{
key scarr.carrname as carrier,
key spfli.connid as flight,
spfli.cityfrom as departure,
spfli.cityto as destination
};    

The classic append view DEMO_CDS_EXTENS is created in ABAP Dictionary. The program DEMO_CDS_VIEW_EXTENSION uses the statement SELECT to access the enhanced view and also displays the components of the dictionary structures in question.

Addition 1

... GROUP BY field1, field2, ...

Effect

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.

Note

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

Example

The following CDS view extension

@AbapCatalog.sqlViewAppendName: 'DEMO_CDS_EXTAGG'
extend view demo_cds_aggregate with demo_cds_extend_aggregate
{
connid,  
sum(distance) as sum_distance
}
group by
connid;    

adds two view fields to the existing CDS view.

@AbapCatalog.sqlViewName: 'DEMO_CDS_AGG'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST,#GROUP_BY]
define view demo_cds_aggregate
as select from
spfli  
{
carrid,
sum(fltime) as sum_fltime
}
group by
carrid;    

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.

Addition 2

... UNION [ALL] { ... }

Effect

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.

Note

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

Example

The following CDS view extension

@AbapCatalog.sqlViewAppendName: 'DEMO_CDS_EXTUNI'
extend view demo_cds_union with demo_cds_extend_union
{
c as c3,
d as c4
}
union
{
f as c3,
g as c4
}
union all
{
k as c3,
l as c4
};    

adds two view fields to the existing CDS view.

@AbapCatalog.sqlViewName: 'DEMO_CDS_UIO'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST,#UNION]
define view demo_cds_union
as select from
demo_join1
{
a as c1,
b as c2
}
union select from
demo_join2
{
d as c1,
e as c2
}
union all select from
demo_join3
{
i as c1,
j as c2
};    

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.



Continue
ABAP CDS - EXTEND VIEW, extension_annot