Exploring ABAP Core Data Services

Objectives

After completing this lesson, you will be able to:

  • Describe the basic tasks of the ABAP Core Data Services.
  • Name the most important ABAP CDS objects.

ABAP CDS Functionality

ABAP Core Data Services (ABAP CDS)

Play the video to learn about ABAP Core Data Services (ABAP CDS).

Let us have a closer look at these tasks.

For a long time, ABAP developers defined views in the ABAP dictionary, in the same way they define database tables. These dictionary views were very limited in terms of SQL logic. They only supported inner joins, a field selection (projection) and simple filter conditions. Note, that the definition of dictionary views is not supported in cloud based ABAP environments.

One of the reasons why SAP introduced Core Data Services in ABAP was the demand for the execution of logic on the database. As you will learn later in this training, ABAP CDS views support a large variety of SQL expressions and functions to push down logic onto the database.

There are two types of CDS views.

CDS DDIC-based views (obsolete)

This obsolete type of CDS views uses a generated dictionary view as technical representation for the database object. CDS DDIC-based views are defined with the DEFINE VIEW statement.

CDS View Entities

This new generation of CDS views is defined with the DEFINE VIEW ENTITY statement. CDS view entities define the database object directly, without generating a dictionary object, first.

Note

In this training, we will only cover CDS view entities. For the older view types, refer to the ABAP keyword documentation.

ABAP CDS not only supports the technical definition of database objects. It also allows you to enrich the technical definitions with semantic information.

Let us have a look at an example.

CDS view entity Z00_C_Employee is defined with a define view entitystatement in a CDS data definition. This is the technical definition of the view, and it is used to create a view named Z00_C_EMPLOYEE on the database. In addition to the define view entity statement, the source code contains annotations. Annotations are code elements that enrich the technical view definition with semantic information. Annotations start with an at-sign (@).

Only a few annotations are evaluated by the ABAP system. The rest is simply passed on to the consumers of the CDS entity, that is, the frameworks that use the CDS entity to read data. The consumers decide by their own which annotations they evaluate and which they ignore.

The most important consumers of ABAP CDS entities are modern UI technologies like SAP Fiori and SAP S/4HANA embedded analytics.

The heart of the ABAP RESTful Application Programming Model is the Business Object.

The structure of the Business Object is modeled with one or more CDS view entities. A CDS behavior definition is used to add the transactional logic. The transactional logic is implemented in an ABAP behavior pool, an ABAP class in the ABAP source code library.

Business Objects are often consumed by Business Services, for example OData services. But you can also access them in ABAP code, using the Entity Manipulation Language (EML).

Note

The details of modeling, implementing and using Business Objects are not part of this training. They are discussed in a dedicated course on the ABAP RESTful Application Programming Model.

ABAP CDS Objects

Important CDS development objects are as follows:

CDS Data Definition

A CDS data definition contains the definition of one CDS entity. The most important CDS entities are CDS view entities and their predecessors, the now obsolete DDIC-based views.

But there are many other CDS entity types that you can define in a data definition:

  • CDS projection views are based on a CDS view and project a subset of its elements.
  • CDS Abstract Entities describe only type properties and for which no database objects are instantiated.
  • CDS Table Functions are implemented in database functions and return a tabular result set.
  • CDS Custom Entities are implemented manually, for example in ABAP code.
  • CDS Hierarchies define SQL hierarchies from an SQL source.

Note

In this training, we will focus on CDS View entities. For details on the other CDS entities, refer to the ABAP documentation.

CDS Access Control

CDS access controls contain the definition of a CDS role. When CDS SQL entities that are assigned a CDS role are accessed, additional access conditions are evaluated by default.

CDS Metadata Extension

CDS metadata extensions are used to store CDS annotations for a CDS entity outside its data definition.

CDS Behavior Definition

CDS Behavior Definitions define the transactional behavior of a business object.

 

In the Project Explorer of ABAP development tools, you find the CDS objects under the development package, in a dedicated node Core Data Services.

Analyze Dictionary and CDS Objects

You want to learn how to define objects in ABAP Dictionary and ABAP Core Data Services. In the first step, you prepare the ABAP development environment, create a new package, and analyze some existing objects.

Prerequisites

If you want to do the exercises as described in this learning journey, you need access to a dedicated hands-on practice system. If you already booked a system for one of the other parts of the learning journey, you can keep using this system for the third part. If you did not yet book a hands-on practice system, follow this link for details: https://learning.sap.com/practice-systems/data-modelling-in-ABAP-dictionary-and-ABAP-core-data-services.

In addition to the service key, which you will receive during the booking process, you need a local installation of the Eclipse IDE with the ABAP Development Tools plug-in installed on top. Follow this link for a step-by-step instruction https://developers.sap.com/tutorials/abap-install-adt.html.

Task 1: Prepare the Project

In Eclipse, create an ABAP Cloud Project and add package /LRN/S4D430_EXERCISE to the list of favorite packages. In the ABAP Cloud project, create a new package under the superpackage ZLOCAL (suggested name: ZS4D430_##, where ## is your group number) and add it also to the favorite packages.

Steps

  1. Open Eclipse, switch to the ABAP perspective and create an ABAP Cloud Project.

    Note

    If you just finished the Intermediate ABAP Programming course and already have your ABAP Cloud Project open in Eclipse, you can skip this step.
    1. Open Eclipse and close all tabs.

    2. Choose WindowPerspectiveOpen PerspectiveOther.

    3. In the dialog box, double-click ABAP.

    4. Choose FileNewABAP Cloud Project.

    5. Select Use a Service Key and choose Next.

    6. Choose Import and select the file containing the service key that you have been given.

    7. Choose Open Logon Page in Browser.

    8. On the browser page that opens, choose directaccess.accounts.ondemand.com and log on with the user and password that you have been given.

    9. When you see the message, You have been successfully logged on, close the browser window and return to Eclipse.

    10. To finish creating the project, choose Finish.

  2. Add package /LRN/S4D430_EXERCISE to the list of Favorite Packages.

    1. In the Project Explorer on the left, expand your ABAP cloud project and then subnode Favorite Packages.

    2. Right-click Favorite Packages and choose Add Package ....

    3. In the search field, enter /LRN/S4D430.

    4. From the list of matching items, select /LRN/S4D430_EXERCISE, then choose OK.

  3. In your ABAP Cloud project, create a new package with the attributes listed in the table. When prompted for a transport, choose the transport request in which you are involved. If no transport request is listed, create a new request.

    FieldValue
    NameZS4D430_##, where ## is your group number
    DescriptionModeling in ABAP
    Add to favorite packagesChecked
    SuperpackageZLOCAL
    Package TypeDevelopment
    Software ComponentZLOCAL
    Application ComponentLeave this field blank
    Transport LayerLeave this field blank
    1. In the Project Explorer, right-click on your ABAP Cloud Project and choose NewABAP Package.

    2. Enter the package name ZS4D430_## where ## is your group number.

    3. Enter the description Modeling in ABAP.

    4. Select the checkbox Add to favorite packages

    5. Enter the superpackage ZLOCAL.

    6. Ensure that the Package Type is set to Development.

    7. Choose Next.

    8. Ensure the Software component is set to ZLOCAL.

    9. Ensure that Application component is empty.

    10. Ensure that Transport layer is empty.

    11. Choose Next.

    12. Check if there is a transport request listed under option Choose from requests in which I am involved. If this is the case, choose this option. If the list is empty, choose the option Create a new request and enter a request description, for example ABAP Exercises.

    13. Choose Finish.

Task 2: Analyze Objects

Analyze the content of package /LRN/S4D430_INTRO.

Note

Package /LRN/S4D430_INTRO is a subpackage of package /LNR/S4D430_EXERCISE.

Steps

  1. In the Project Explorer view, expand /LRN/S4D430_EXERCISE/LRN/S4D430_INTRODictionary and analyze the content.

  2. Similarly, expand /LRN/S4D430_EXERCISE/LRN/S4D430_INTROCore Data Services and analyze the content.

Task 3: Explore Usage in ABAP

Create an ABAP class that implements interface IF_OO_ADT_CLASSRUN (suggested name: ZCL_##_INTRO, where ## stands for your group number). Use code completion to explore which objects from package /LRN/S4D430_INTRO you can use as data types and as data source in a SELECT statement.

Steps

  1. In your package, create a new ABAP class (suggested name: ZCL_##_INTRO, where ## stands for your group number).

    1. In the Project Explorer view, right-click your package Z##_S4D430 to open the context menu.

    2. From the context menu, choose NewABAP Class.

    3. Confirm that the Package field contains the name of your package. Enter the name of the new class in the Name field and Analyze dictionary and CDS in the Description field.

    4. Choose Add....

    5. On the dialog window, enter IF_OO_ADT_CLASSRUN and choose OK.

    6. Back on the previous window choose Next.

    7. Confirm the transport request and choose Finish.

  2. In the IF_OO_ADT_CLASSRUN~MAIN method, declare a variable of type string (suggested name: var).

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      METHOD if_oo_adt_classrun~main. DATA var TYPE string. ENDMETHOD.
  3. Replace type string with an object from package /LRN/S4D430_INTRO. Use code-completion to find out which of the objects you can use as data types.

    Note

    All objects in package /LRN/S4D430_INTRO have a name that starts with /LRN/S4D430_IND.
    1. Remove string.

    2. Enter /LRN/S4D430_IND and press Ctrl + Space to invoke code-completion.

  4. Implement a SELECT statement that reads from database table /LRN/S4D430_IND.

    Hint

    To keep it simple, read all fields (FIELDS *), all rows (no WHERE clause), and use an inline declaration in the INTO TABLE clause.
    1. Add the following code:

      Code Snippet
      Copy code
      Switch to dark mode
      12345
      SELECT FROM /lrn/s4d430_ind FIELDS * INTO TABLE @DATA(result).
  5. Replace /lrn/S4D430_ind in the FROM clause with another object from package /LRN/S4D430_INTRO. Use code-completion to find out which of the objects you can use as data source.

    Note

    All objects in the /LRN/S4D430_INTRO package have a name that starts with /LRN/S4D430_IND.
    1. In the FROM clause, place the cursor immediately after /LRN/S4D430_IND.

    2. Press Ctrl + Space to invoke code-completion.

Log in to track your progress & complete quizzes