Getting Started with SAP ABAP Core Data Services (CDS)
Learn how to use SAP ABAP Core Data Services (CDS) with detailed step-by-step explanations and examples. Improve your SAP ABAP development skills with this comprehensive guide on CDS.
Table of Contents
- What is CDS?
- Advantages of ABAP CDS
- Creating CDS Views
- CDS Associations
- Creating OData Service with CDS
- Conclusion
1. What is CDS?
Core Data Services (CDS) is a framework used for data modeling and data access. CDS allows us to perform complex queries and operations at the database level in a simpler and more performant way. ABAP CDS integrates this concept with the ABAP language, enabling us to optimize data access and processing workflows.
2. Advantages of ABAP CDS
- Performance Improvement: Since it operates at the database level, data operations are faster.
- Reusability: Defined CDS views can be used in different applications.
- Central Data Model: Provides ease of maintenance and management.
- Rich Annotation Support: You can add additional information for UI, OData services, and more.
3. Creating CDS Views
3.1 Basic CDS View
Step 1: Open a project with Eclipse or ABAP Development Tools (ADT).
Step 2: Create a new CDS view.
- Right-click on the project > New > ABAP Repository Object > Core Data Services > Data Definition
Step 3: Name your CDS view and add the following code.
@AbapCatalog.sqlViewName: 'ZCUSTVIEW'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer Basic Data'
define view Z_Customer_View
as select from kna1 {
key kunnr as CustomerNumber,
name1 as Name,
land1 as Country,
ort01 as City,
pstlz as PostalCode
}
Description:
- @AbapCatalog.sqlViewName: Specifies the name of the physical view to be created in the database.
- @AccessControl.authorizationCheck: Sets the authorization check.
- @EndUserText.label: Adds a description for the view.
- define view: Defines the CDS view.
- select from kna1: Selects data from the KNA1 (Customer Master) table.
3.2 Using Annotations
Annotations allow us to add additional information to CDS views and are used for various purposes.
Example: Adding UI Annotations
@AbapCatalog.sqlViewName: 'ZCUSTVIEW'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer Basic Data'
@UI.headerInfo: { typeName: 'Customer', typeNamePlural: 'Customers' }
define view Z_Customer_View
as select from kna1 {
key kunnr as CustomerNumber,
@UI.selectionField: [{ position: 10 }]
@UI.lineItem: [{ position: 10 }]
name1 as Name,
land1 as Country,
ort01 as City,
pstlz as PostalCode
}
Description:
- @UI.headerInfo: Sets the header information to be displayed in the UI.
- @UI.selectionField and @UI.lineItem: Determines the ordering and visibility of fields in the user interface.
4. CDS Associations
Associations allow us to establish relationships between CDS views and simplify JOIN operations.
Example: Combining Customer and Order Data
@AbapCatalog.sqlViewName: 'ZCUSTORDVIEW'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer Orders'
define view Z_Customer_Order_View
as select from Z_Customer_View as Customer
association [0..*] to vbak as _Orders on $projection.CustomerNumber = _Orders.kunnr
{
key Customer.CustomerNumber,
Customer.Name,
Customer.Country,
_Orders
}
Description:
- association [0..*]: Establishes a relationship with multiple orders.
- _Orders: Alias representing the association.
Accessing Associated Data
Instead of using LEFT OUTER JOIN
, you can directly use the association to access related data.
define view Z_Customer_Order_Detail_View
as select from Z_Customer_Order_View
association [0..*] to vbap as _OrderItems on _Orders.vbeln = _OrderItems.vbeln
{
key CustomerNumber,
Name,
_Orders.vbeln as OrderNumber,
_OrderItems.posnr as ItemPosition,
_OrderItems.matnr as MaterialNumber,
_OrderItems.kwmeng as OrderQuantity
}
5. Creating OData Service with CDS
You can publish CDS views as OData services.
Step 1: Add OData Annotation to the CDS View
@AbapCatalog.sqlViewName: 'ZCUSTODATA'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer Data OData Service'
@OData.publish: true
define view Z_Customer_OData
as select from kna1 {
key kunnr as CustomerNumber,
name1 as Name,
land1 as Country,
ort01 as City,
pstlz as PostalCode
}
Step 2: Activate the OData Service
- Without needing to use transaction
SEGW
, the service is automatically created. - Use transaction
/IWFND/MAINT_SERVICE
to activate the service.
Step 3: Testing the Service
- Open the following URL in your web browser:
https://<server_address>:<port>/sap/opu/odata/sap/ZCUSTODATA_CDS/
6. Conclusion
ABAP CDS allows you to manage data modeling and data access in SAP systems in a modern and efficient way. With CDS, you can write high-performance, low-maintenance, and reusable code. Features like annotations and associations enable you to enrich your data models and simplify your integrations with OData services.