Working with CDS View

Objectives

After completing this lesson, you will be able to:

  • Analyze a CDS view definition.
  • Read data using a CDS view.

CDS Views

View Definitions in ABAP Core Data Services (ABAP CDS)

ABAP Core Data Services, or ABAP CDS for short, is an infrastructure for defining and consuming semantically rich data models in ABAP. They use a CDS DDL to define CDS entities that implement a semantic data model. The most important CDS modeling entities are CDS View entities.

The CDS View definition contains re-usable SQL logic; sometimes as simple as a projection of table fields and sometimes more sophisticated with calculations, aggregations, joins, unions, and so on.

A CDS View definition can contain associations to reflect the relations of the data model. Consumers of the view can use the associations to retrieve related data.

Finally, annotations are used to semantically enrich the view definition. This metadata is evaluated by frameworks that build on top of CDS View definitions. One such framework is the ABAP RESTful application programming model , for which we will discuss an example later in this course.

Let's watch examples of CDS view definitions.

CDS View definitions are contained in repository objects of type Data Definition. Let us now have a look at the source code of data definition /DMO/I_CONNECTION.

The main part is the DEFINE VIEW ENTITY statement. It contains the name of the CDS View entity and, after keyword FROM, the data source. In our example, the name of the view entity is /DMO/I_Connection and the data source is database table /dmo/connection. Optional addition AS, defines an alias name Connection to address the data source inside the View definition.

Note

The source of a CDS View entity could also be another CDS View.

A pair of curly brackets contains the list of view elements. In our example, the view elements are fields of database table /dmo/connection. Keyword Key in front of the first two elements defines them as key fields of the CDS View entity. Optional addition AS defines an alias name for each view element.

Addition association defines a relation to another CDS view entity. In our example, the related is CDS view entity /DMO/I_Carrier and the name of the association is _Airline.

This association becomes available for consumers of the view by adding it to the element list. This is referred to as exposing the association.

Annotations start with the at sign (@) and they are used to semantically enrich the view definition for consumers. Annotations before the view definition are called entity annotations. Entity annotations are used to define metadata for the view entity as a whole. Annotations between the curly brackets are called element annotations. Element annotations are used to define metadata for the different elements of the view.

You already learned how to use the Data Preview tool to display and analyze the content of a database table. This tool is also available for CDS view entities.

To open the Data Preview for a given CDS entity, right-click anywhere in the data definition and choose Open With > Data Preview. Alternatively, place the cursor anywhere in the database table definition and press Ctrl + F8.

The tool displays the data returned by the CDS entity. The same functions are available to sort or filter the data and adjust the display.

If the view definition contains one or more associations, you can use them to display related data. To do so, proceed in the following manner:

  1. Right-click on a row in the display.
  2. From the context menu, choose Follow Association.
  3. From the list of available associations, choose the one in which you are interested.

If you want to find all CDS Views with a certain database table as a source, you can utilize the Where-used List tool of ADT. To use this tool, proceed as follows:

  1. Open the definition of the database table.
  2. Right-click anywhere in the source code and choose Get Where-used List from the context menu. Alternatively, you can press Ctrl + Shift + G, or choose the button from the toolbar with the same symbol.
  3. The Search view displays a list of all development objects that directly use the database table.

You can apply filters to the Where-used List if, for example, you are only interested in objects from certain packages or objects of the specific object type. The example illustrates how to filter for CDS views that use the table.

How to Analyze a CDS View

CDS Views in ABAP SQL

When you implement a SELECT statement in ABAP, you can use a CDS view entity as the data source instead of reading from the database table directly. This has several advantages.

  • Re-use of the SQL logic contained in the CDS view
  • Concise, easy-to-read reading of related data using the associations
  • Sometimes the names of views and view elements are better readable than the more technical names of database tables and table fields

The SELECT statement in the example uses CDS view entity /DMO/I_Connection as a data source.

Note

Note that the names used in the FIELDS clause and WHERE conditions are the alias names of the view elements.

The third element in the FIELDS clause makes use of exposed association _Airline. It reads element name from the associated CDS view entity /DMO/I_Airline. This kind of element is called a path expression. The backslash (\) is a mandatory prefix for association names.

Analyze and Use a CDS View Entity

In this exercise, you read from a CDS view entity rather than from the database table directly. This provides you with comfortable access to the name of the airline as well.

Template:

  • /LRN/CL_S4D400_DBS_SELECT (global Class)

Solution:

  • /LRN/CL_S4D400_DBS_CDS (global Class)

Task 1: Copy Template (Optional)

Copy the template class. If you finished the previous exercise, you can skip this task and continue editing your class ZCL_##_SELECT.

Steps

  1. Copy the class /LRN/CL_S4D400_DBS_SELECT to a class in your own package (suggested name: ZCL_##_CDS, where ## stands for your group number).

    1. Open the source code of the global class /LRN/CL_S4D400_DBS_SELECT.

    2. Link the Project Explorer view with the editor.

    3. In the Project Explorer view, right-click the class /LRN/CL_S4D400_DBS_SELECT to open the context menu.

    4. From the context menu, choose Duplicate ....

    5. Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_CDS, where ## stands for your group number.

    6. Adjust the description and choose Next.

    7. Confirm the transport request and choose Finish.

Task 2: Analyze CDS View Entity

Analyze the definition of the CDS view entity /DMO/I_Connection.

Steps

  1. Open the development object that contains the definition of the CDS view entity /DMO/I_Connection.

    1. From the eclipse toolbar, choose Open ABAP Development Object or press Ctrl + Shift + A.

    2. In the input field, enter /DMO/I_Con as a search string.

    3. In the list of matching items, click on /DMO/I_CONNECTION (Data Definition) and choose OK.

  2. Analyze the element list of the CDS view entity.

    1. Navigate to the comma-separated list between the pair of curly brackets ( { } ).

    2. You find the alias names after keyword AS.

  3. Open the Tooltip Description for the target of association _Airline.

    1. Click on /DMO/I_Carrier beforeas _Airline and press F2 to show the tooltip description.

Task 3: Declare Additional Attribute

Extend the local class lcl_connection with a private instance attribute carrier_name. Add some output for the new attribute to the implementation of method get_output( ).

Note

The new attribute is not filled, yet. In order to fill it we will adjust the SELECT statement in the next task of this exercise.

Steps

  1. Return to the local class lcl_connection in your global class.

    1. In the global class, choose Local Types.

  2. Add the following private attribute to the class definition:

    Attributes

    Attribute NameScopeData Type
    carrier_nameinstance/DMO/CARRIER_NAME
    1. Add the highlighted code:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678910
      PRIVATE SECTION. DATA carrier_id TYPE /dmo/carrier_id. DATA connection_id TYPE /dmo/connection_id. DATA airport_from_id TYPE /dmo/airport_from_id. DATA airport_to_id TYPE /dmo/airport_to_id. ENDCLASS.
  3. Extend the implementation of method get_output. Add the carrier name to the string template with the carrier identification.

    1. Navigate to the implementation of method get_output.

    2. Replace statement APPEND |Carrier:     { carrier_id      }| TO r_output. with the following statement:

      Code Snippet
      Copy code
      Switch to dark mode
      123
        APPEND |Carrier:     { carrier_id } { carrier_name }| TO r_output.
  4. Activate the class. Execute it and analyze the console output.

    1. Press Ctrl + F3 to activate the class.

    2. Press F9 to run the class.

Task 4: Use CDS View Entity

In the implementation of method constructor, replace the SELECT statement that reads from database table /dmo/connection with a SELECT statement that reads from CDS view entity /DMO/I_Connection.

Steps

  1. In the implementation of method constructor, comment the SELECT statement.

    1. Select all lines that belong to the SELECT SINGLE ... ..

    2. Press Ctrl + < to add a star sign (*) in front of each selected line.

  2. After the commented code, add a SELECT statement that reads a single record from database table /DMO/I_Conncetion.

    1. Add the highlighted code:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678
      * SELECT SINGLE * FROM /dmo/connection * FIELDS airport_from_id, airport_to_id * WHERE carrier_id = @i_carrier_id * AND connection_id = @i_connection_id * INTO ( @airport_from_id, @airport_to_id ).
  3. Implement the FIELDS clause. Read view elements DepartureAirport and DestinationAirport. In addition, read view element Name from the target of association _Airline.

    Hint

    Use auto-completion (Ctrl + Space) to enter the names.
    1. After FROM /DMO/I_Connection enter FIELDS.

    2. After a blank, press Ctrl + Space and choose DepartureAirport.

    3. After a comma and a blank press Ctrl + Space again and choose DestinationAirport.

    4. After a comma and a blank, type in a backslash (\) , press Ctrl + Space and choose _Airline.

    5. Immediately after _Airline, type in a dash sign (-) , press Ctrl + Space and choose Name.

    6. The complete FIELDS clause should look like this:

      Code Snippet
      Copy code
      Switch to dark mode
      123
      FIELDS DepartureAirport, DestinationAirport, \_Airline-Name
  4. Implement the WHERE condition. Restrict the key elements of CDS view entity with the values of importing parameters i_carrier_id and i_connection_id. Do not forget to escape the parameters with prefix @.

    Hint

    Use auto-completion (Ctrl + Space) to enter the element names and parameter names.
    1. Add the following code after the FIELDS clause:

      Code Snippet
      Copy code
      Switch to dark mode
      1234
      WHERE AirlineID = @i_carrier_id AND ConnectionID = @i_connection_id
  5. Implement the INTO clause. Store the SELECT result in attributes airport_from_id, airport_to_id, and airline_name. Do not forget to escape the attributes with prefix @.

    Hint

    Use auto-completion (Ctrl + Space) to enter the attribute names.
    1. Add the highlighted code:

      Code Snippet
      Copy code
      Switch to dark mode
      123
      WHERE AirlineID = @i_carrier_id AND ConnectionID = @i_connection_id
    2. The complete SELECT statement should look like this:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678
      SELECT SINGLE FROM /DMO/I_Connection FIELDS DepartureAirport, DestinationAirport, \_Airline-Name WHERE AirlineID = @i_carrier_id AND ConnectionID = @i_connection_id INTO ( @airport_from_id, @airport_to_id, @carrier_name ).
  6. Activate the class. Execute it and analyze the console output. Check that the output for the new attributes displays data.

    1. Press Ctrl + F3 to activate the class.

    2. Press F9 to run the class.

Log in to track your progress & complete quizzes