How to Create and Activate a Basic CDS View in SAP S/4HANA

CDS (Core Data Services) views in SAP S/4HANA allow you to define data models at the database level and make data access much simpler and more efficient. They are designed to replace traditional ABAP views by offering richer features, better integration with SAP HANA, and a more flexible data modeling capability. CDS views are particularly useful for creating virtual data models for analytical and transactional applications within SAP.

Business Scenario

Imagine you work for a retail company, and the sales team needs a real-time view of sales data, such as the quantity and value of products sold. Instead of accessing multiple tables and performing complex joins, you can create a CDS view to fetch the required data from a single source efficiently. In this tutorial, we’ll go through the steps of creating and activating a basic CDS view in SAP S/4HANA to provide the sales team with an accessible data model.

Step-by-Step Solution to Create and Activate a Basic CDS View in SAP S/4HANA

Step 1: Access the ABAP Development Tools (ADT) in Eclipse

To create a CDS view, you’ll need access to the ABAP Development Tools in Eclipse. SAP recommends using ADT in Eclipse because CDS views are created and managed within the Eclipse environment.

  1. Open Eclipse and install the ABAP Development Tools (ADT) if they are not already installed.
  2. Connect to your SAP system by setting up an ABAP project. You’ll need to provide your system ID, client, username, and password.
  3. Once connected, you’re ready to begin creating the CDS view.

Step 2: Create a New CDS View

  1. Navigate to your ABAP package: In the Project Explorer, locate the ABAP package where you want to create the CDS view. Right-click on the package and select New > Other.
  2. Choose Core Data Services: In the wizard, type “CDS” in the search box, and choose Data Definition under the Core Data Services folder. Click Next.
  3. Define the CDS view details: Provide a name for your CDS view, like Z_SALES_DATA_VIEW. Make sure it follows the naming conventions for custom objects (starting with Z or Y).
  4. Provide a description: Give a brief description of what the CDS view represents, such as “Sales Data View for Sales Analytics.”

Step 3: Define the CDS View Syntax and Select Data Source

After creating the CDS view, Eclipse will generate a template where you can define the data source and fields.

Define SQL View Name: Every CDS view has a corresponding SQL view, which is the name of the view in the database. Use the @AbapCatalog.sqlViewName annotation to specify the name.

ABAPCopy code@AbapCatalog.sqlViewName: 'ZSALESVIEW'

Specify Authorization Check: Use the @AccessControl.authorizationCheck annotation to control data access. For this example, we’ll set it to #NOT_REQUIRED:

ABAPCopy code@AccessControl.authorizationCheck: #NOT_REQUIRED

Define the View Label: Set a label for the CDS view for clarity:

ABAPCopy code@EndUserText.label: 'Sales Data View for Sales Analytics'

Define the Select Statement: Write the SQL SELECT statement to pull data from the source tables, selecting the fields you need.

Here’s a sample code snippet for a basic CDS view that fetches sales data from a table VBAK (Sales Document: Header Data) and includes fields like Sales Document Number, Document Date, Net Value, and Currency.

ABAPCopy codedefine view Z_SALES_DATA_VIEW as select from vbak
{
    key vbak.vbeln      as SalesDocument,       // Sales Document Number
    vbak.erdat          as DocumentDate,        // Document Date
    vbak.netwr          as NetValue,            // Net Value
    vbak.waerk          as Currency             // Currency
}

Step 4: Save and Activate the CDS View

  1. Save the CDS view by pressing Ctrl + S or clicking File > Save.
  2. Right-click on the CDS view file in the Project Explorer and select Activate. Eclipse will compile the CDS view, creating the underlying SQL view in the SAP HANA database.

Step 5: Test the CDS View

To validate that your CDS view is returning the correct data, test it using the Data Preview option in Eclipse.

  1. Data Preview: Right-click on your CDS view file and select Open With > Data Preview. You should see the data returned by your CDS view in the Data Preview window.
  2. Filtering and Sorting: Use filters or sorting options in the preview window to ensure the data is retrieved accurately and meets the requirements of your scenario.

Transaction Codes and Testing Tips

  • CDS Test Data Preview: After activation, you can test your CDS view using transaction SE11 to check the underlying SQL view.
  • ABAP CDS Views in SAP GUI: You can access and test your CDS views using the transaction SE80 as well.

Interview Tips on CDS Views

  1. Understand CDS Annotations: Be prepared to explain the purpose of common CDS annotations like @AbapCatalog.sqlViewName, @AccessControl.authorizationCheck, and @EndUserText.label.
  2. CDS and Performance: Interviewers may ask how CDS views enhance performance in SAP HANA. Explain how CDS views leverage SAP HANA’s in-memory processing for faster data retrieval.
  3. Use Cases for CDS Views: Expect questions on business scenarios for CDS views, such as creating analytical or transactional data models for various functional areas (sales, finance, inventory).
  4. Difference between CDS Views and ABAP Views: Be ready to discuss the benefits of CDS views over traditional ABAP views, like support for complex calculations, associations, and annotations.

Final Thoughts

Creating a basic CDS view in SAP S/4HANA allows you to present data in a clean and accessible way for applications. In this example, we created a CDS view to provide real-time sales data to the sales team, but CDS views can be expanded for complex scenarios using advanced annotations and SQL functions. Understanding how to model data effectively with CDS is essential for SAP developers looking to leverage SAP HANA and SAP S/4HANA’s advanced data capabilities.

Leave a Comment