CDS Views in ABAP
Core Data Services (CDS) Views are an integral part of
SAP’s next-generation data modeling infrastructure. These views are more
advanced than traditional database views (DDIC views) and are designed to take
full advantage of the in-memory capabilities of SAP HANA. They form the
foundation of the modern ABAP programming model, particularly in SAP S/4HANA,
which is built around a virtual data model (VDM) architecture.
What
is a CDS View?
A CDS View is a reusable data model that defines the
projection of data from one or more database entities. Unlike traditional
database views, CDS views go beyond simple projections and joins by allowing
advanced modeling capabilities directly at the database level.
CDS views are not persistent entities; they are virtual
models that represent relationships within a database. This allows for
real-time data access and minimizes the data load on the application server,
thanks to the concept of code pushdown, which ensures data-intensive operations
are handled directly by the HANA database.
Why
Use CDS Views?
CDS views have numerous benefits:
- Code Pushdown: With CDS, operations are executed at the
database level, reducing load on the application server.
- VDM: SAP S/4HANA uses CDS in the form of the Virtual
Data Model, which allows for optimized data consumption.
- Reusable Data Models: CDS views are reusable and
provide a clear separation between the data model and application logic.
- Enhanced Capabilities: CDS views go beyond DDIC views
with advanced functions like associations, joins, and parameters.
Key
Features of CDS Views:
- CDS views are part of the ABAP programming model.
- Defined and consumed at the database level, not the
application level.
- Used in conjunction with SAP HANA to harness real-time,
in-memory capabilities.
Basic
Structure of CDS Views
The structure of a CDS view involves annotations that
define its behavior, such as the name, description, and security checks. Here’s
a breakdown:
1. @AbapCatalog.sqlViewName: Defines the corresponding
SQL view name in the ABAP Dictionary (SE11).
2. @AbapCatalog.compiler.compareFilter: Enables
comparison filters at the database level.
3. @AbapCatalog.preserveKey: Ensures that key fields are
preserved in the generated view.
4. @EndUserText.label: Provides a description for the CDS
view.
5. @AccessControl.authorizationCheck: Defines the level
of authorization check required for the CDS view.
Example: Simple
CDS View
Let’s look at a simple CDS view that selects material
data from the MARA table:
abap
@AbapCatalog.sqlViewName:
'ZCDSVIEW'
@AbapCatalog.compiler.compareFilter:
true
@AbapCatalog.preserveKey:
true
@AccessControl.authorizationCheck:
NOT_REQUIRED
@EndUserText.label:
'simple cds view'
define view
zcds_view1 as select from mara
{
key mara.matnr,
mara.mtart
}
- The annotation @AbapCatalog.sqlViewName specifies that
a corresponding SQL view named 'ZCDSVIEW' will be created in SE11.
- The key fields mara.matnr and mara.mtart are selected
from the MARA table.
Steps
to Create a CDS View
To create a CDS view in SAP, follow these steps:
1. Open Eclipse (ADT): CDS views are created using the
ABAP Development Tools (ADT) in Eclipse, not in SE80.
2. Create a New Data Definition: Go to File > New >
ABAP Repository Object and select Core Data Services > Data Definition.
3. Define the CDS View: Enter the annotations, source
table, and fields you want to select.
4. Activate the CDS View: Once the view is defined,
activate it. The SQL view will be created automatically in the ABAP Dictionary
(SE11) with the name specified in the annotation @AbapCatalog.sqlViewName.
How
CDS Views Appear in the ABAP Dictionary (SE11)
When a CDS view is created and activated, the name
specified in the @AbapCatalog.sqlViewName annotation is used to create a
corresponding SQL view in the ABAP Dictionary (SE11). This allows the CDS view
to be treated like any other database view in SAP.
Types
of CDS Views
1. Simple CDS View: Selects data from a single table.
2. CDS View with Joins: Combines data from multiple
tables using SQL-like joins.
3. CDS View with Associations: Establishes relationships
between entities without hard joins.
4. CDS View with Parameters: Accepts input parameters to
dynamically filter the data.
5. Extend View: Extends an existing CDS view by adding
fields.
6. Table Functions: CDS views that define database
functions.
Types
of CDS Views in Detail:
- Simple View: A basic CDS view that selects data from a
single table.
- View with Joins: Allows you to combine multiple tables
using joins.
- View with Associations: Establishes a relationship
between two entities without performing an actual join until the data is
queried.
- View with Parameters: Allows you to pass dynamic values
into the view to filter data.
- Extend View: Used to extend the functionality of an
existing CDS view by adding more fields or logic.
- Table Functions: Special CDS views that behave like
database functions, returning a table based on the logic within the view.
Advantages of CDS Views over Traditional DDIC
Views
- Performance: CDS views execute at the database level,
taking full advantage of SAP HANA’s in-memory architecture.
- Modularization: CDS views are reusable across
applications and other views.
- Code Pushdown: Heavy lifting is done by the database,
not the application server.
- Flexibility: CDS views support complex joins,
associations, and calculations that go beyond what traditional views offer.
Conclusion
CDS views are a powerful and flexible tool for modern
data modeling in SAP. They allow ABAP developers to push down data-intensive
operations to the HANA database, improving performance and enabling real-time
data processing. With the advent of SAP S/4HANA and CDS views, SAP has taken a
major step forward in database and application development.
CDS views are central to the new ABAP programming model,
enabling high-performance, in-memory data processing. Understanding how to
create, manage, and optimize CDS views is key to developing modern, efficient
SAP applications.
Code Recap:
abap
@AbapCatalog.sqlViewName:
'ZCDSVIEW'
@AbapCatalog.compiler.compareFilter:
true
@AbapCatalog.preserveKey:
true
@AccessControl.authorizationCheck:
NOT_REQUIRED
@EndUserText.label:
'simple cds view'
define view
zcds_view1 as select from mara
{
key mara.matnr,
mara.mtart
}