0% found this document useful (0 votes)
805 views33 pages

Templates For The CDS View

Uploaded by

Jamila Hamdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
805 views33 pages

Templates For The CDS View

Uploaded by

Jamila Hamdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Templates for the CDS View

Table of Contents

Table of Contents 2

Overview 2

Templates for the CDS View 3


Create a New Template 5

ABAP CDS - DDL for Data Definitions 8


CDS DDIC-based entities 9
● Define View 9
● Define View with Joins 10
● Define View with Associations. 12
● Define View with Parameters 14
ABAP CDS - CDS Entities 16
ABAP CDS - View Entities 17
ABAP CDS - Projection Views 18
ABAP CDS - Table Functions 21
ABAP CDS - Hierarchies 24
ABAP CDS - Custom Entities 26
ABAP CDS - Common DDL Elements 30
ABAP CDS - CDS Entity Extension 30
■ EXTEND VIEW ENTITY 30
■ EXTEND CUSTOM ENTITY 30
■ EXTEND ABSTRACT ENTITY 31
■ EXTEND VIEW 31

2
Overview

During the ABAP CDS View development, we spent many hours writing same
DDL source code.

Eclipse based ADT provides Data Definition Templates, to address the above
problem and reduce the time spent on ABAP CDS view development.

When you create an ABAP CDS view in ADT and follow the Data Definition
creation wizard, when you reach the step Templates, where you will find the
predefined code templates available for selection.

Templates for the CDS View

Eclipse ADT IDE provides 12 predefined ABAP CDS view templates. You can
view all data definition templates available in Window -> Preference section.

3
You can view the preview of Data Definition Template by selecting any of the
template.

You create additional Data Definition templates for your own use by
creating, editing and removing templates. Lets see how we can do it.

4
Create a New Template

1. Under Templates section, click on New.

2. On New Template dialog window. Specify the Name and the Description,
and select Data Definition (creation) as the Context.

3. Choose the Insert Variable button to add variables in the Pattern field.

Add annotations and other fields as per need to prepare your template. Here
I created the template with all annotations I need for SAP Fiori Elements
application.

5
4. Click on OK.

5. Click on Apply and Close button to save the template.

6
6. Finally our new template is ready for use. Lets create a ABAP CDS view
based on newly created template. Select the custom template

7. DDL source code is pre-populated based on the template, all you have to
do is to replace the table and field names.

7
ABAP CDS - DDL for Data Definitions

The CDS data definitions in ABAP CDS are created using the data definition
language CDS DDL in DDL source code. The CDS DDL follows the general CDS
syntax rules for ABAP CDS and comprises the following statements for the
definition and enhancement of CDS entities and CDS DDIC-based entities:

■ Statements for CDS entities:


● DEFINE VIEW ENTITY
● DEFINE VIEW ENTITY AS PROJECTION ON
● DEFINE TABLE FUNCTION
● DEFINE HIERARCHY
● DEFINE CUSTOM ENTITY
● DEFINE ABSTRACT ENTITY
■ Statements for CDS DDIC-based entities:
● DEFINE VIEW
■ Language elements that can be used in various CDS entities:

8
● Entity annotations
● Input parameters
● Typing
● Element annotations
■ Statements for CDS entity extensions:
● EXTEND VIEW ENTITY
● EXTEND ABSTRACT ENTITY
● EXTEND CUSTOM ENTITY
● EXTEND VIEW

CDS DDIC-based entities

● Define View
Defines a CDS view in the ABAP CDS in a CDS source code. A CDS is
implemented using a query select_statement. The annotation
AbapCatalog.sqlViewName must be specified before the view itself is defined
using DEFINE VIEW.

Two objects are created for a CDS view that is defined using DEFINE VIEW. A
name must be specified for each of the two objects:

● The name CDS_DB_VIEW of the CDS database view must be specified in


quotation marks after the annotation @AbapCatalog.sqlViewName.
This view is the technical foundation of the CDS view in ABAP
Dictionary. The usual rules for ABAP Dictionary views apply to this
name and it is not case-sensitive (it is transformed internally into
uppercase letters). The associated SQL view is created under this name

9
on the database. The name given to the database view can no longer
be changed after the CDS view is transported into a follow-on system.
● The name cds_entity of the CDS entity is defined after the keywords
DEFINE VIEW (DEFINE is optional). No quotation marks need to be
specified. This name follows the rules of the CDS database view, but
can have 30 characters. The CDS entity represents all properties of the
CDS view.

● Define View with Joins

Defining a join between two data sources of a CDS view in ABAP CDS.using
join expression.

Both inner and outer joins are possible:

● A join between two data sources using INNER JOIN or just JOIN selects
all entries of the data sources whose fields meet the ON condition.
● A join between two data sources using LEFT OUTER JOIN selects all
entries on the left side. A join between two data sources using RIGHT
OUTER JOIN selects all entries on the right side. Entries that meet the
ON condition have the same content as in the inner join. In entries that
do not meet the ON condition, the elements on the right or left side

10
have the null value that is set to a type-friendly initial value when the
CDS view is used in Open SQL.

In nested join expressions, the order of the evaluation is specified by the


arrangement of the ON conditions. From left to right, the most adjacent ON
conditions are assigned to each JOIN and this expression is parenthesized
implicitly. These implicit parentheses can be made explicit using actual
parentheses, ( ). This is optional.

In every join expression, a join condition cond_expr must be specified after


ON. When specified, special rules apply to this condition.

➔ Rules for conditions cond_exp in an ON condition of a join of a CDS view in


ABAP CDS:

1. All relational operators are allowed.

2. lhs expects a field of the data source data_source that represents

the left side of the join.

3. A field of both data sources data_source of the join, a literal with

11
optional domain prefix, a parameter, or a session variable can be

specified for rhs (with the exception of the operator LIKE).

4. At least one comparison for equality between a field of the left

data source and a field of the right data source of the join must

be performed.

5. No path expressions or other expressions or function calls can be


specified.

● Define View with Associations.

Defining an association of the name _assoc in a SELECT statement of a CDS


view. An association connects the first elementary data source data_source
specified as the initial data source (after FROM using the ON condition
cond_exp) to the data source data_source specified as the target data source
(in the definition of the association). The target data source cannot be built
using joins.

An association of a SELECT statement can be accessed – in the same


statement at all places where this is documented – by specifying the
association name in path expressions. When a CDS view is activated with
path expressions, the specified associations are converted to join
expressions. The initial data source is shown on the left side and the target
data source is shown on the right side. The ON condition of the association is
added to the ON condition of the join. The type of join depends on the place
where the path expression is used.

12
When specifying the ON condition, the following special rules apply:

● Fields of the initial data source, which are specified in the ON


condition, must also be listed in the SELECT list of the current SELECT
statement. This ensures that a join expression can be built from the
association (when used in a path expression). In the ON condition, the
field name should be prefixed by $projection and not by the name of
initial data source.
● The fields of the target data source must be prefixed in the ON
condition by the name of the association (prefix assoc. separated by a
period).
➔ Rules for conditions cond_exp in an ON condition of an association:

1. The relational operator IS [NOT] NULL is not allowed.

2. A field of one of the two data sources data_source of the association can
be specified for lhs.

13
3. A field of one of the two data sources data_source of the association or a
literal can be specified for rhs.

4. At least one comparison for equality between a field of the initial data
source and a field of the target data source of the association must be
performed.

5. No path expressions or other expressions or function calls can be


specified.

● Define View with Parameters

Defining a parameterized view with $parameters.pname: data type/data


element in a SELECT statement of a CDS view. As a developer, you can now
parameterize your CDS views. It means you can define one general views
which produce context-specific result sets by using parameter values passed
at execution time. This means that you do no longer need to create a view for
each context!

Sometimes users need to enter parameters to influence the results


according to business needs. This is the case, for example, if the user wants
to filter the results on a language, or if they want to modify the results of a
calculation by entering the percentage of a discount coupon. They are
defined through the WITH PARAMETERS clause. When the view is executed,
the user will be asked for these parameters IN THE pop_up.

Defines Input parameters pname1,pname2 in a CDS View in ABAP CDS in a comma separated
list. Each input parameter must be typed with a data type parameter type.

14
In above shown example, here in zcds_view CDS view carid and conid are
used as parameters to get refined result based on filter values given by user
at the time of execution. In WHERE clause the parameters are mapped to
data source fields used in CDS view.

An input parameter called carid can be used as an operand in the following

places in the SELECT statement of the view using the syntax


$parameters.carid:

1. Right side of an expression cond_exp in a WHERE clause or HAVING clause.

2. Right side of an expression cond_exp in an ON condition in an ABAP join

or an association.

3. Right side of an expression cond_exp in a filter condition of a path

expression.

15
Note:- The CDS views with parameters are not supported on all SAP certified
databases – At least SAP HANA does support it 🙂 . However, the DDL of the
ABAP CDS allows creating and accessing CDS views with parameters
independent of the database system.

ABAP CDS - CDS Entities


The following sections describe the possible CDS entities:

■ CDS view entities


■ CDS projection views
■ CDS table functions
■ CDS hierarchies
■ CDS custom entities

16
■ CDS abstract entities

ABAP CDS - View Entities

A CDS view entity is a CDS entity defined using the statement DEFINE VIEW ENTITY in the CDS DDL of
ABAP Core Data Services (CDS). A CDS view entity can be used to select fields from one or more data
sources (database tables or other CDS entities). Additional semantic properties can be added by
using annotations. CDS view entities are the successor of CDS DDIC-based views (obsolete). They
have the same structure and serve the same purpose as CDS DDIC-based views (obsolete). Generally
spoken, they represent an improved version of "classic" CDS DDIC-based views (obsolete).

When a CDS view entity is activated, a repository object is created. This repository object can be
accessed using the name of the CDS view entity. As a global data type, each CDS view entity
represents a structured type with the elements of the CDS view entity as components and can be
used like any CDS entity:

■ In ABAP CDS, the CDS entity can be used as a data source of other CDS entities.
■ In ABAP programs, the CDS entity can be used as a data type and in ABAP SQL read
statements.
■ The CDS entity cannot be used as a data type for definitions of dictionary objects.

The name of the CDS entity is in the namespace of all global types of an AS ABAP.

The following sections describe client handling and table buffering for CDS view entities. Afterwards,
the statement DEFINE VIEW ENTITY and all possible additions are described.

■ Client handling
■ Table buffering

17
■ Creation of a CDS view entity

ABAP CDS - Projection Views

A CDS projection view is defined in the DDL source code of a CDS data
definition using the statement DEFINE [TRANSIENT] VIEW ENTITY AS
PROJECTION ON in the CDS DDL of the ABAP Core Data Services (CDS).

18
A CDS projection view is based on a CDS entity and exposes a subset of its
features.

The name of a CDS projection view is in the namespace of all global types of
an AS ABAP.

Client handling is done implicitly and automatically for CDS projection views.
The rules are the same as for CDS view entities, see section ABAP CDS - Client
Handling in CDS View Entities.

Table buffering does not work for CDS projection views, since buffering in
general does not work with another CDS view as data source.

There are different types of projection views available, depending on the


purpose and use case of the projection view. The type of projection view is
specified using the syntax addition PROVIDER CONTRACT. The following
types of CDS projection views are available:

■ CDS transactional queries


Transactional queries define the projection layer of a RAP business
object.
For details, see the section about Transactional Queries.
■ CDS transactional interfaces
CDS transactional interfaces serve as stable public interface layer in a
CDS data model. They are typically used in the context of the ABAP
RESTful Application Programming Model to provide the basis for a RAP
BO interface.
For details, see the section about CDS Transactional Interfaces.
■ CDS analytical queries
CDS analytical queries are intended for modelling analytical queries

19
within the CDS data model. They are evaluated by the analytical engine.
For details, see the section about Analytical Queries.
■ no provider contract specified
Deprecated. It is strongly recommended that a provider contract is
specified. If no provider contract is specified, the same features are
available as for transactional queries. There are only a few differences
in the syntax checks to transactional queries. The differences are listed
in topic no contract.

20
ABAP CDS - Table Functions

A CDS table function is defined in the DDL source code of a CDS data
definition in the ABAP Development Tools using the statement DEFINE TABLE
FUNCTION in the CDS DDL of the ABAP Core Data Services (CDS). A CDS table
function includes the following:

■ The CDS entity


A CDS table function is declared as a CDS entity table_function using
the statement DEFINE TABLE_FUNCTION. As a global data type, the CDS
entity represents a structured type with the elements of the CDS table
function as components and can be used like any CDS entity.
● In ABAP CDS, the CDS entity can be used as a data source of
other CDS entities.
● In ABAP, the CDS entity can be used as a data type and in ABAP
SQL read statements.
● The CDS entity cannot be used as a data type for definitions of
dictionary objects.
■ A CDS table function is located in the namespace of all global types of
an AS ABAP.
■ An AMDP function implementation
CDS table functions are implemented in platform-dependent SQL in a
special AMDP method and the implementation is managed as an AMDP
table function by the AMDP in the database system.
The AMDP method is specified after the addition IMPLEMENTED BY in
the definition of the CDS table function using DEFINE
TABLE_FUNCTION. It must be declared as a special AMDP function
implementation for exactly one CDS table function using the addition

21
FOR TABLE FUNCTION.
The parameter interface of the AMDP table function is defined using
the input parameters and the element list of the CDS table function.

A CDS table function is client-dependent by default and implicit client


handling is applied in ABAP SQL reads.

This is an example of Table function with parameters.

22
The figure below presents a definition of AMDP function for Table function
with parameters.

23
Notes:-

● CDS table functions can only be used in a database system that


supports AMDP.
● When a CDS table function is created, the CDS entity must be activated
first, before the associated AMDP function implementation is created.
● When a CDS table function is transported, the CDS entity is first
transported as part of the dictionary transport objects and then the
AMDP function implementation as part of the ABAP transport objects.
Depending on the size of the transport, there can be a considerable
delay between these two phases where the CDS table function is not in
a usable state.

ABAP CDS - Hierarchies

A CDS hierarchy is defined in the DDL source code of a CDS data definition in the ABAP Development
Tools using the statement DEFINE HIERARCHY in the CDS DDL of the ABAP Core Data Services (CDS).
The name of the CDS hierarchy must match the name of the DDL source code.

A CDS hierarchy represents an SQL hierarchy created when the CDS hierarchy is accessed. CDS
hierarchies can currently only be used when the standard database is a SAP HANA database. When a
CDS hierarchy is activated, a view with the same name is created as an ABAP-managed database
object on the database. This view accesses a hierarchy generator function such as HIERARCHY.

A CDS hierarchy is declared as a CDS entity hierarchy using the statement DEFINE HIERARCHY. As a
global data type, the CDS entity represents a structured type with the elements of the CDS hierarchy
as components and can be used like any CDS entity.

■ In ABAP CDS, the CDS entity can be used as a data source of other CDS entities.
■ In ABAP, the CDS entity can be used as a data type and in ABAP SQL read statements.
■ The CDS entity cannot be used as a data type for definitions of dictionary objects.

24
A CDS hierarchy is located in the namespace of all global types of an AS ABAP.

When using CDS hierarchies, the handling of the special hierarchy attributes is of special
importance. These attributes occur as part of the result set of the hierarchy function on the
database:

■ The hierarchy attributes can be accessed explicitly as follows:


● The prefix $node. can be used to list them in the element list of the CDS hierarchy
and grant them an alias name. They are then applied like a regular element of the
CDS hierarchy.
● When a ABAP SQL query is applied to a CDS hierarchy, they are handled as additional
hierarchy columns, regardless of their element list, and their names can be used in
explicitly specified columns
■ This makes it possible to specify a hierarchy attribute in the element list of the CDS hierarchy
in two different ways in an ABAP SQL query, either by using the name of the element or by
using the name of the hierarchy column.
■ When CDS hierarchies are used, the hierarchy attributes are not respected implicitly in the
following cases:
● When used as a data source of other CDS entities.
● When used as a data type.
● When used as a data source in ABAP SQL, if * or ...~* is specified in the SELECT list.

25
■ In these cases, only the explicit elements of the element list are respected. These can,
however, represent hierarchy attributes.

ABAP CDS - Custom Entities

A CDS custom entity is defined in the DDL source code of a CDS data definition using the statement
DEFINE CUSTOM ENTITY in the CDS DDL of the ABAP Core Data Services (CDS). The name of the CDS
custom entity must match the name of the DDL source code.

Purpose and use cases:

CDS custom entities allow developers to implement their own data provision using ABAP. Data
retrieval is implemented via an ABAP class and performed at runtime. On CDS level, only an
interface is defined, while the implementation of the logic is encapsulated in the application server

26
artifact. Custom entities are used if data access cannot be done via means of SQL and the available
CDS feature set and other CDS entity types. For example, custom entities can be used when

■ data is stored in non-relational storage, for example in a BLOB.


■ data is stored in SAP liveCache and access is done via ABAP APIs.
■ data cannot be computed by means of CDS due to additional application-server-side logic.
■ a data model can be defined via CDS views, but the DCL feature set is not sufficient to define
the necessary authorizations.

Data retrieval is also possible using an http client.

In the context of the ABAP RESTful Application Programming Model, custom entities are used as a
data model basis for RAP unmanaged queries.

A CDS custom entity represents all type properties of a CDS entity:

■ Metadata defined using CDS annotations


■ A structured type defined using an element list
■ Optionally, a parameter interface defined using input parameters
■ Any CDS associations, CDS compositions, or CDS to-parent associations exposed in the
element list.

A CDS custom entity can be used as follows:

■ ABAP CDS
● A CDS custom entity can be specified as the association target of a CDS association,
CDS composition, or CDS to-parent association. A CDS association, CDS composition,
or CDS to-parent association of this type can be exposed only and cannot be created
as an instance of a join expression. Such associations can only be followed in Fiori
OData scenarios where the RAP query engine (not SQL) is used to execute the query.
● Since no SQL artifact is generated for CDS custom entities and the query is
implemented in ABAP, a CDS custom entity cannot be used as a data source of
SELECT statements or used by SQL path expressions in other CDS entities.
■ ABAP Programs
● The structured type of the CDS custom entity is known in ABAP programs. Like the
type of any CDS entity, it can be used after the addition TYPE.
● A CDS custom entity cannot be used as a data source of SELECT statements or used
by path expressions in ABAP SQL.
■ Frameworks

27
● In the context of the ABAP RESTful Application Programming Model, custom entities
are used as a data model basis for RAP unmanaged queries. The ABAP query is in
this case executed by a framework such as the RAP query engine.
● The annotations of a CDS custom entity can be evaluated using the methods of the
class CL_DD_DDL_ANNOTATION_SERVICE.
● The parameters and the CDS associations of CDS custom entities can be evaluated
by frameworks such as the RAP query engine.

An instance of a CDS custom entity is not created as a database object and client handling is not
defined.

ABAP CDS - Abstract Entities

28
A CDS abstract entity is defined in the DDL source code of a CDS data definition in the ABAP
Development Tools (ADT) using the statement DEFINE ABSTRACT ENTITY in the CDS DDL of the ABAP
Core Data Services (CDS).

A CDS abstract entity represents all type properties of a CDS entity:

■ Metadata defined using CDS annotations


■ A structured type defined using an element list
■ Optionally a parameter interface defined using input parameters
■ Any CDS associations, CDS compositions, or CDS to-parent associations exposed in the
element list

An instance of a CDS abstract entity is not created as a database object and client handling is not
defined. A CDS abstract entity can be used as follows:

■ ABAP CDS
● A CDS abstract entity can be specified as the association target of a CDS association,
CDS composition, or CDS to-parent association. A CDS association, CDS composition,
or CDS to-parent association of this type can be exposed only and cannot be created
as an instance of a join expression.
● A CDS abstract entity cannot be used as a data source of SELECT statements or used
by SQL path expressions in other CDS entities, such as CDS view entities or
DDIC-based CDS views.
■ ABAP Programs
● The structured type of the CDS abstract entity is known in ABAP programs. Like the
type of any CDS entity, it can be used after the addition TYPE.
● A CDS abstract entity cannot be used as a data source of SELECT statements or used
by path expressions in ABAP SQL.
● The statement ANNOTATE ENTITY can be used to add CDS metadata extensions to
the annotations of a CDS abstract entity.
■ Frameworks
● The annotations of a CDS abstract entity can be evaluated using the methods of the
class CL_DD_DDL_ANNOTATION_SERVICE.
● The parameters and the CDS associations of CDS abstract entities can be evaluated
by frameworks such as the RAP framework.

29
ABAP CDS - Common DDL Elements

The following language elements can be used in various CDS entities:

■ Entity annotations
■ Input parameters
■ Typings
■ Element annotations

ABAP CDS - CDS Entity Extension

A CDS entity extension is a transportable extension of a CDS entity. A CDS


entity extension can make additions to the original entity, but it cannot
modify, overwrite, or delete elements from the original entity. There are
currently four different statements that can be used to extend a CDS entity in
CDS DDL:

■ EXTEND VIEW ENTITY


● This variant is used to define a CDS view entity extension for a
CDS view entity or a CDS projection view.
● Available since release 7.78.

■ EXTEND CUSTOM ENTITY


● This variant is used to define a CDS custom entity extension for a
CDS custom entity.
● Available since release 7.89.

30
■ EXTEND ABSTRACT ENTITY
● This variant is used to define a CDS abstract entity extension for
a CDS abstract entity.
● Available since release 7.84.

■ EXTEND VIEW
● This variant is used to define a CDS view extension that is also
linked with a DDIC append view in ABAP Dictionary.
● Available since release 7.40, SP08.
● Recommended for extending CDS DDIC-based views (obsolete),
defined using the statement DEFINE VIEW.

When working in a restricted ABAP language version, extensions are possible


only to released APIs that are released under the C0 stability contract for
extensions. The compatibility rules for the C0 contract for API providers and
API consumers are described in section Compatibility Rules for CDS Entities.

31
Note:-

The following CDS views cannot currently be extended:

● Views with an explicit name list.


● Views with aggregate expressions and a GROUP-BY clause.
● Views with a UNION clause for union sets.

The CDS view extensions themselves cannot be extended.

32
33

You might also like