ABAP CDS Development User Guide 1726926497
ABAP CDS Development User Guide 1726926497
PUBLIC
Warning
This document has been generated from the SAP Help Portal and is an incomplete version of the official SAP product
documentation. The information included in custom documentation may not re ect the arrangement of topics in the SAP Help
Portal, and may be missing important aspects and/or correlations to other topics. For this reason, it is not for productive use.
This is custom documentation. For more information, please visit the SAP Help Portal 1
8/27/2024
Overview
ABAP CDS provides a framework for de ning and consuming semantic data models on the central database of the application
server AS ABAP. The speci ed data models are based on the data de nition language (DDL) and the data control language
(DCL). So, a CDS entity or the extension of a CDS view is de ned as source code in the data de nition.
Remember
In the DDL editor, you can only de ne one ABAP CDS entity in one data de nition or metadata extension.
Related Information
ABAP CDS in ABAP Dictionary (ABAP Keyword Documentation)
A CDS view is de ned for existing database tables and views, or for other CDS views in the ABAP Dictionary, using the DEFINE
VIEW DDL statement. A CDS view serves to de ne the structure of a CDS database view and represents a projection onto one
or several database tables or database views in the ABAP Dictionary.
Note
CDS database views and CDS entities are part of one and the same namespace. Therefore, you must assign different names
for a CDS database view and the entity.
This is custom documentation. For more information, please visit the SAP Help Portal 2
8/27/2024
Example
@AbapCatalog.sqlViewName: 'CUSTOMER'
DEFINE VIEW cust_book_view_entity AS SELECT FROM scustom
JOIN sbook
ON scustom.id = sbook.customid
{
scustom.id,
scustom.name,
sbook.bookid
}
The cust_book_view_entity CDS entity de nes a projection onto the database tables scustom and sbook by joining
both tables. The generated CDS database view (CUSTOMER) comprises the ID, the name, and the booking ID of all customers
for which the bookings exist.
Example
CLASS cl_demo_access_cds_entity IMPLEMENTATION.
...
METHOD get_data.
This is custom documentation. For more information, please visit the SAP Help Portal 3
8/27/2024
SELECT id name bookid
FROM cust_book_view_entity
INTO TABLE @DATA(result_data)
WHERE ... .
ENDMETHOD.
...
ENDCLASS.
Note
When activating a data de nition, a CDS entity and CDS database view form a unity with the data de nition as development
object. So, after transporting the data de nition, the name of the CDS entity and CDS database view can no more be
changed. To rename any part of this unity, you need to delete the corresponding data de nition. Consequently, you recreate
it and use the new name for the relevant part.
This is custom documentation. For more information, please visit the SAP Help Portal 4
8/27/2024
Developer-Relevant Activities
1. Creating Data De nitions
7. [Optional:] Analyzing the SQL dependency tree of the view in case of more complex CDS views
9. [Optional:] Analyzing the relationship between views in a graphical tool in case of more complex views
Caution
Before deleting DDL, check whether it is still being used by other development objects. To nd out if an object is still in
use, call the where-used function(Searching Usages (Where-Used)). See also: Deleting Development Objects
Related Information
ABAP CDS - View (ABAP Keyword Documentation)
Extend Views
The actual CDS entity of the table function that is generated in the ABAP Dictionary
Note
This is custom documentation. For more information, please visit the SAP Help Portal 5
8/27/2024
In contrast to the CDS views, the CDS table functions can be implemented using Native SQL. This implementation is done
within an AMDP method of an AMDP class and is managed as an AMDP function by the AMDP framework in the database
system.
Note
The name of the implementing AMDP method can only be speci ed in a single CDS table function (1: 1 relation).
Example
Table Function De nition
In the following listing, a client-speci c ABAP CDS table function TAB_FUNCTION_EXAMPLE is de ned using the DDL syntax.
This table function declares two input parameters clnt (with the prede ned value: #CLIENT) and carrid, and a list of
elements that provide the return values of the AMDP method that implements the table function. The table function is
associated with the AMDP class CL_EXAMPLE_AMDP, where the method GET_FLIGHTS is used to implement the table
function.
Sample Code
@ClientDependent: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
define table function TAB_FUNCTION_EXAMPLE
This is custom documentation. For more information, please visit the SAP Help Portal 6
8/27/2024
Example
Table Function Implementation
The public ABAP class (AMDP class) in this example provides the AMDP method get_flights, which serves as the
implementation of the table function tab_function_example. As with any other AMDP class, cl_example_amdp
must implement the marker interface IF_AMDP_MARKER_HDB. The AMDP method get_flights implements the data
selection using Native SQL code.
Sample Code
class cl_example_amdp definition public.
public section.
interfaces IF_AMDP_MARKER_HDB.
class-methods get_flights for table function tab_function_example.
protected section.
private section.
endclass.
endclass.
Developer-Relevant Activities
1. Creating Data De nitions
Related Information
This is custom documentation. For more information, please visit the SAP Help Portal 7
8/27/2024
ABAP CDS Synatx – Table Functions (ABAP Keyword Documentation)
Access Controls
ABAP Core Data Services (CDS) has its own authorization concept CDS access controls using a data control language (DCL).
The authorization concept of ABAP CDS uses conditions de ned in CDS and can draw upon classical (PFCG) authorizations to
check the authorizations of users.
The CDS authorization concept coexists with the classical authorization concept of SAP NetWeaver Application Server for ABAP
(SAP NetWeaver AS for ABAP). You can use the concepts together or independently from another. The classical authorization
concept is based on authorization objects. The authorization of a user occurs either implicitly, for example while calling a
transaction, or explicitly with the statement AUTHORITY-CHECK. The CDS authorization concept is based on implicit
authorization checks that occur during access attempts to CDS entities over Open SQL.
A developer de nes a CDS role in a separate CDS source code for a CDS entity using the DCL statement DEFINE ROLE. When
a CDS entity is accessed using Open SQL, the following is checked:
If no CDS role is de ned for a CDS entity, there are no restrictions on the data returned by the query.
Note
There are cases where a CDS role is de ned, but it is not taken into account at runtime.
The developer of the DDL source has set the annotation @AccessControl to #NOT_ALLOWED.
The DDL has been inherited by another CDS entity. Only the CDS role for the parent DDL is relevant at
runtime. If the parent DDL has no CDS role or the annotation @AccessControl has been set to
#NOT_ALLOWED, then the CDS role is masked and ignored at runtime.
The developer has added the keywords WITH PRIVILEGED ACCESS in the FROM clause of an Open SQL query.
If a CDS role is de ned for the CDS entity, access control management checks the current user for authorizations. The
system only reads data for which an authorization exists. CDS roles are assigned to all users implicitly.
When you activate an access control, SAP NetWeaver AS for ABAP generates the artifacts access control management needs
and saves them in the ABAP runtime environment. At runtime, an application accesses a CDS entity using Open SQL and ABAP
adds the restrictions to the selection conditions.
This is custom documentation. For more information, please visit the SAP Help Portal 8
8/27/2024
Notes
We recommend that you continue to use the classical authorization concept for start authorizations. Start authorizations check
whether a user can start an application in the rst place. The CDS authorization concept can be used within an application to
perform instance-based authorization checks. Instance-based authorization checks the authorization of a user as de ned by the
data model and the data in question.
Related Information
Adding Access Controls to CDS Entities
ABAP CDS - Access Control (ABAP Keyword Documentation)
CDS Annotations
A CDS annotation (or annotation for short) enables you to add ABAP and component-speci c metadata to the source code of
any CDS entity.
Use
You can use code completion ( Ctrl + Space ) to add annotations directly in a data de nition, for example, before the
define statement or within a select list in a CDS view. The validity of the annotation then depends on the corresponding
position where you use it. If they are added at the wrong position, the source editor will mark and underline them in red.
In addition, you can use annotations in metadata extensions to de ne customer-speci c metadata for a CDS view without
modifying SAP's CDS entities itself. When using metadata extensions, you can overwrite speci c annotation values de ned in a
data de nition or add additional annotation values to an entity. Note that you can only use those annotations in metadata
extensions that are not relevant when activating CDS entities.
Example
This is custom documentation. For more information, please visit the SAP Help Portal 9
8/27/2024
@AbapCatalog.sqlViewName: 'CUSTOMER'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@Metadata.allowExtensions: true
DEFINE VIEW cust_book_view_entity
AS SELECT FROM scustom
JOIN sbook
ON scustom.id = sbook.customid
{
@EndUserText.label: 'Customer ID'
scustom.id,
@EndUserText.label: 'Customer Name'
scustom.name,
@EndUserText.label: 'Customer Booking ID'
sbook.bookid
}
The example from above demonstrates how you can use annotations and at which positions you can add annotations:
Annotations that are used before the define view statement are valid for the whole cust_book_view_entity
CDS view:
The @EndUserText.label annotation used before an element in the select list provides a text for the
corresponding eld.
Activation
Errors resulting from the use of component annotations do not prevent activation or creation of a CDS entity at the rst time.
They are only evaluated if the activation of the entity was successful.
Component annotations can result in the generation of other ABAP repository objects.
Example
An OData service is generated when using the @OData.publish: true annotation. In this case, the annotation is
highlighted with a marker that provides additional information about the generated object.
Note
When the CDS entity is activated, the OData service is generated automatically. After activation, it can be opened from the
ABAP Element Information popup of the corresponding database table. To do this, select the underlined "OData-Service" link
in the Generated Object section.
This is custom documentation. For more information, please visit the SAP Help Portal 10
8/27/2024
Subsequently, the OData service also needs to be activated in the transaction /IWFND/MAINT_SERVICE manually.
Related Information
ABAP CDS - Annotations (ABAP Keyword Documentation)
CDS Annotations (Framework-Speci c Reference Documentation)
Annotation Propagation
Active Annotations View
Displaying Annotation Values of an Active CDS View
Extracting CDS Annotations to a Metadata Extension
Activate OData Service in the SAP Gateway Hub
Annotation Propagation
The values of CDS annotations can be inherited and merged between CDS entities.
Use
You as a developer have the following possibilities to use annotations in order to provide metadata in your data model:
Use metadata extensions to enrich a CDS entity with customer-speci c annotation values
You can build hierarchies when selecting data from other CDS views. In accordance to this hierarchy and the corresponding
elements in the select list, the annotation values are propagated from bottom to top. In addition, you can also use metadata
extensions. Note that metadata extensions can also re ect a hierarchy when assigning several metadata extensions to a CDS
view.
You can assign customer-speci c metadata through annotations in one or more metadata extensions to one data de nition.
The precedence of the annotations contained in the metadata extensions is determined by the layer of the extension. For this
the following values are provided:
Value Description
This is custom documentation. For more information, please visit the SAP Help Portal 11
8/27/2024
Value Description
Example
@Metadata.layer: #CUSTOMER
Note
All annotations provided in metadata extensions are compounded with the
annotations in the corresponding data de nition. Element annotations
(scope ELEMENT) are propagated in the view hierarchy.
You use the Annotation Propagation view to display the currently active and inactive values of CDS annotations and the CDS
entities from which these values have been propagated in accordance to the current position of the cursor in the DDL editor.
Source CDS entity or metadata extension from which the value of a CDS annotation originates.
If you provide several metadata extensions for a data de nition, you can reproduce how metadata extensions provide
metadata on different layers.
After generating, all involved annotation values and their corresponding data sources are listed. The effective entries are
highlighted in black. The metadata that is ignored is highlighted grey. Based on this list, you can now check which values are
considered from your data de nition.
You can also adapt the selection of the data source at any time. To do this, choose the corresponding Browse... button in the
Selection area. Select then the relevant data source or key.
Example
The following example visualizes merging annotations through the ABAP API:
This is custom documentation. For more information, please visit the SAP Help Portal 12
8/27/2024
Precedence of the metadata contained in the data definitions and metadata extensions is as follows: <CUSTOMER Extension 2> >
<PARTNER Extension 2> > <CDS View 2> > <CUSTOMER Extension 1> > <CORE Extension 1> > <CDS View 1>
The active annotations returned by the API for the elds of the CDS View <CDS View 2> are as follows:
Related Information
ABAP CDS - Evaluation of Annotations (ABAP Keyword Documentation)
ABAP CDS - Evaluation of Metadata Extensions (ABAP Keyword Documentation)
Annotation Propagation View
Analyzing Annotation Propagations
This is custom documentation. For more information, please visit the SAP Help Portal 13
8/27/2024
You can use the following CDS view extensions to extend CDS entities:
Extend Views to add new elements to a CDS view from its underlying data source or de ne new associations for the CDS
view.
Metadata Extensions to overwrite existing or add new CDS annotations to a one or more elements or parameters of a
CDS entity.
Overview
The following example shows you how to extend a CDS view:
Example
In the select list of the cust_book_view_entity CDS view:
The metadata of the scustom.id, scustom.name, and scustom.bookid elds is overwritten by the metadata
extension. When the corresponding data de nition is consumed, the metadata of the metadata extension is taken
into account.
The scustom.street and scustom.city database elds are added through the extend view. When you select
data from the corresponding data de nition, the data of these database elds will also be retrieved.
This is custom documentation. For more information, please visit the SAP Help Portal 14
8/27/2024
Possibilities to overwrite existing CDS annotations as well as to add elements to a CDS view
After creating a CDS view extension, the indicator is added at the define view statement to indicate that the select
list of the view has been extended.
Marker that indicates that the select list of the CDS view has been extended
Metadata Extensions
As of SAP NetWeaver AS for ABAP 7.51 innovation package, you can use metadata extenstions to add customer-speci c
annotations to SAP's CDS entities. Note that these changes do not result in modi cations.
De nition
A metadata extension is a development object that provides CDS annotations in order to extend the CDS annotations used in a
CDS view. The standard ABAP Workbench functions (transport, syntax check, activation, and so on) are supported.
Use
This is custom documentation. For more information, please visit the SAP Help Portal 15
8/27/2024
Metadata extensions enable you to write the annotations for a CDS view in a different document to separate them from the
CDS view.
Overview
The metadata of CDS views are not extensible by default.
To use a metadata extension for a CDS view, you have to consider the following conditions:
1. In the de nition of the CDS view, the @Metadata.allowExtensions annotation with the value true is added. This
annotation explicitly allows the use of metadata extensions.
2. In the metadata extension, you have to de ne the name of the CDS view to be annotated in the annotate view
statement.
Advantages
You can bene t from the following advantages using metadata extensions:
1. Separation of Concerns: Separating the metadata speci ed in the annotations from the implementation of the view:
In addition, the metadata can be developed and updated independently of the data de nition.
2. ABAP Dictionary-independent activation: When activating a CDS view, the metadata extensions will be ignored. This
results in the following advantages:
It reduces the number of ABAP Dictionary (mass) activations required to develop and maintain the CDS view.
It facilitates changing the metadata of a CDS view in a running system, thereby reducing downtime.
3. Modi cation-free enhancements: Customers, partners, and industries can customize the metadata without modifying
the CDS view.
In addition, metadata extensions are switchable. This means, the metadata can be speci cally enabled or disabled
depending on the use case.
Activation
In general, in a metadata extension only those annotations are permitted that do not affect the ABAP Dictionary
activation/generation or the activation/generation of secondary objects (for example, OData services). For example, the ABAP
annotation @EndUserText and the component-speci c annotations @UI can be speci ed in metadata extensions. A syntax
error occurs if annotations that are not permitted are speci ed.
Related Information
This is custom documentation. For more information, please visit the SAP Help Portal 16
8/27/2024
ABAP CDS Metadata Enhancements (ABAP Keyword Documentation)
Extracting CDS Annotations to a Metadata Extension
Creating Metadata Extensions
Annotation Propagation
Extend Views
An extend view is the extension of an existing CDS view using the extend view statement. The extend view itself is not a CDS
entity.
Use
When extending, you add further element(s) from the data source (such as a database table or another CDS view) to the select
list of an existing CDS view. So, you can enrich a CDS view that is, for example, part of the SAP standard without resulting in
modi cations.
Entry to add the Extend View template through the creation wizard of data definitions
In this template, the following placeholders are provided and need to be adapted:
${sql_view_append_name}: Name of the append view to be created in the ABAP Dictionary when activating the extend
view
This is custom documentation. For more information, please visit the SAP Help Portal 17
8/27/2024
${view_name}': Name of the CDS view to be extended
${data_source_name}: Name of the data source from which you want to add new elements to the CDS view
Sample Code
@AbapCatalog.sqlViewAppendName: 'DEMO_EXT_VIEW'
@EndUserText.label: 'Demo'
extend view Demo_Data_Model_Base with Demo_Extend_View {
spfli.countryfr as CountryFrom,
spfli.countryto as CountryTo
}
The Demo_Extend_View extend view extends the select list of the Demo_Data_Model_Base CDS view with the
countryfr and countryto elds. This data is now also provided whenever the Demo_Data_Model_Base CDS view is
used.
an append view that represents the added eld(s) is created in the ABAP Dictionary
the extended eld(s) is added to the existing CDS view on the database
Note
They are then
considered for every occurrence where the extended CDS view is used.
Related Information
ABAP CDS - EXTEND VIEW (ABAP Keyword Documentation)
Creating Extend Views
Creating Data De nitions
This is custom documentation. For more information, please visit the SAP Help Portal 18
8/27/2024
In order to test the logic in CDS entity under test (CUT), it is required to insert test data into the double. This data is
returned by the Test Doubles when the CUT is executed. CUT must not be modi ed by the test framework to enable unit or
hierarchical testing. For example, the test frameworks should not modify the view de nition to exclude elds, joins, lters and so
on to enable certain testing aspects of its runtime behavior.
1. Creating "updatable" Test Doubles for each dependent component in the same database schema. The framework
double that gets created is a stub which has the same structure as the original dependent component:
2. Creating a Clones of the CUT wherein the actual logic of the CUT is preserved and the actual entites of the DOC are
replaced by the corresponding Test Doubles.
This is custom documentation. For more information, please visit the SAP Help Portal 19
8/27/2024
Related Information
ABAP CDS Unit Testing
ABAP CDS Hierarchical Testing
Glossary
The development object for CDS unit testing is a CDS view for which the runtime entity is in database layer (SQL View). Using
CDS Test Double Framework APIs available in ABAP unit class, you can write an ABAP unit test for a CDS View for runtime
artifacts that are in two different layers. You can also create test doubles for database entities.
Note
The double has the same structure as the original dependent component.
Copies depended-on database tables, but do not copy its data and primary key constraints
Creates database tables for depended-on database views. These tables have the same structure as the
depended on database views
Copies depended-on database functions (resulting from depended-on CDS views with parameters) and modi es
the implementation of function double to "insert" desired test-data into the double
DDIC tables
DDIC views
CDS views
This is custom documentation. For more information, please visit the SAP Help Portal 20
8/27/2024
CDS views with Parameters
External Views
Table Functions
Note
You can also turn on/off DCL for a given CDS. We recommend you to turned off DCL to truly isolate the CDS during unit
testing.
Related Information
Unit Testing in ABAP
Writing Unit Tests Using CDS Test Double Framework
You must select exact one dependency anywhere in each hierarchy path. You must choose the database dependencies in the
CDS View Hierarchy for which you wants to create Doubles for. The framework creates the Doubles for the dependent
database entities that are selected. For the database entities in the hierarchy path, that are above of these selected entities,
framework creates Clones entities. These cloned entities preserve the logic in the entity. However, these cloned entities point
to either the other Clones or Doubles as its dependencies than the actual database entities.
The database entities in the hierarchy path that are below these selected entities are not considered.
Related Information
Writing Hierarchy Test using CDS Test Double Framework
This is custom documentation. For more information, please visit the SAP Help Portal 21