How to Create a CDS View in SAP ABAP
Creating a Core Data Services (CDS) View in SAP ABAP involves defining a data model that is executed at the database level, allowing you to define, select, and manage data with improved performance and scalability. Below is a step-by-step guide on how to create a CDS view in ABAP Development Tools (ADT) for Eclipse.
Steps to Create a CDS View in SAP ABAP
Step 1: Open Eclipse with ABAP Development Tools (ADT)
- Launch Eclipse and switch to the ABAP Perspective.
- Go to
Window > Perspective > Open Perspective > Other...
and select ABAP.
- Connect to your SAP system using your system credentials.
- Ensure that the required ABAP projects are created and available.
Step 2: Create a New CDS View
- Go to the ABAP Repository in the Eclipse workspace.
- Right-click on the package where you want to create the CDS view.
- Select New > Other from the context menu.
- In the wizard, search for ABAP CDS View and select it.
Step 3: Define the Basic Information
- Enter the View Name: Enter a unique name for your CDS view.
- Convention: Typically, the name starts with
Z
(for custom objects) orY
.
- SQL View Name (optional): You can specify a SQL view name, but it is not required when using
DEFINE VIEW ENTITY
for new CDS views. - Package: Specify the ABAP package to store the CDS view or leave it as
LOCAL
if it’s for testing purposes. - Transport Request: Select a transport request for version management.
Click Finish to create the view.
Step 4: Define the CDS View Logic
Once you have created the CDS view, you can define its logic in the editor. A CDS view generally includes:
- Select statement: The main query that retrieves data.
- Field definitions: The fields or columns selected from one or more database tables.
- Annotations: For UI (e.g., Fiori apps) or security.
Example: Basic CDS View Definition
@AbapCatalog.sqlViewName: 'ZFLIGHT_CDS'@AccessControl.authorizationCheck: #NOT_REQUIREDdefine view ZFLIGHT_CDS as select from sflight { key carrid, key connid, fldate, price}
Explanation:
@AbapCatalog.sqlViewName
: Defines the SQL view name (optional, mainly used for older CDS views).@AccessControl.authorizationCheck
: Defines if authorization checks are required (use#CHECK
or#NOT_REQUIRED
).define view ZFLIGHT_CDS
: The main syntax to define the CDS view, selecting from thesflight
table.
Step 5: Enhance the CDS View with Associations or Calculated Fields (Optional)
You can extend your CDS view to include associations (for relationships between entities), calculated fields, or aggregations. Here’s how you can enhance your view:
Example: Adding an Association
define view ZFLIGHT_DETAILS as select from sflightassociation [0..1] to scarr as _Carrier on _Carrier.carrid = sflight.carrid { key sflight.carrid, key sflight.connid, sflight.fldate, sflight.price, _Carrier.name as airline_name}
Example: Adding a Calculated Field
define view ZFLIGHT_DISCOUNT as select from sflight { key carrid, key connid, fldate, price * 0.9 as discounted_price}
- Association: Joins the
sflight
table with thescarr
(carrier) table to get the airline name. - Calculated Field: Adds a field
discounted_price
, which applies a 10% discount to theprice
.
Step 6: Activate the CDS View
- Right-click on the newly created CDS view in the project explorer.
- Select Activate.
- This action generates the view in the database and makes it available for use.
Step 7: Test the CDS View
After activation, you can test the view:
- Use the SE11 transaction in the SAP GUI to test the view.
- Query the view using SQL or ABAP code.
You can also expose the CDS view as an OData service if needed (for Fiori apps or external consumers).
🔹 Example of a Complete CDS View with Annotations
Here’s an example of a more complex CDS view with annotations for UI display and aggregation:
@AbapCatalog.sqlViewName: 'ZFLIGHT_CDS_AGGR'@AccessControl.authorizationCheck: #CHECK@UI.selectionField: [{ position: 10 }]define view ZFLIGHT_CDS_AGGR as select from sflight { key carrid, key connid, fldate, sum(price) as total_revenue}group by carrid, connid
@UI.selectionField
: Defines which fields are shown as filter options in Fiori UI.sum(price)
: Performs an aggregation to calculate the total revenue for each flight.
🔹 Benefits of Using CDS Views
- Better Performance: CDS views are executed on the database level, reducing the need for processing on the application server.
- Flexibility: Easily integrate annotations for business logic, UI formatting, and security.
- Simplified Data Modeling: CDS views enable you to model complex relationships (associations) and calculated fields without having to write complex joins or SQL queries manually.
- Upgrade Safe: By using CDS views, you ensure that your logic is tied to the database rather than application logic, improving future compatibility.