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)

  1. Launch Eclipse and switch to the ABAP Perspective.
  1. Connect to your SAP system using your system credentials.
  2. Ensure that the required ABAP projects are created and available.

Step 2: Create a New CDS View

  1. Go to the ABAP Repository in the Eclipse workspace.
  1. In the wizard, search for ABAP CDS View and select it.

Step 3: Define the Basic Information

  1. Enter the View Name: Enter a unique name for your CDS view.
  1. 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.
  2. Package: Specify the ABAP package to store the CDS view or leave it as LOCAL if it’s for testing purposes.
  3. 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:

Example: Basic CDS View Definition

@AbapCatalog.sqlViewName: 'ZFLIGHT_CDS'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view ZFLIGHT_CDS as select from sflight {
key carrid,
key connid,
fldate,
price
}

Explanation:


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 sflight
association [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
}

Step 6: Activate the CDS View

  1. Right-click on the newly created CDS view in the project explorer.
  2. Select Activate.

Step 7: Test the CDS View

After activation, you can test the view:

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

🔹 Benefits of Using CDS Views

  1. Better Performance: CDS views are executed on the database level, reducing the need for processing on the application server.
  2. Flexibility: Easily integrate annotations for business logic, UI formatting, and security.
  3. 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.
  4. Upgrade Safe: By using CDS views, you ensure that your logic is tied to the database rather than application logic, improving future compatibility.