Defining a Basic CDS View

Objectives

After completing this lesson, you will be able to:

  • Create a data definition.
  • Maintain the FROM clause and element list.

Creating a Data Definition

To create a new data definition, proceed as follows:

  1. Right-click on your project in the Project Explorer.
  2. Choose NewABAP Repository Object …
  3. From the list, choose Core Data ServicesData Definitions.

    Hint

    You can use the input field below Project to type a search term, for example data def.

  4. Choose Next.
  5. Enter the name of an existing package, and a name and description for the new data definition.

    Hint

    If you enter the name of a database table or the name of a CDS View entity as Referenced Object, the new view will read all fields from that referenced object.

If your package already contains a data definition, you can use the following shortcut:

  1. In the Project Explorer, expand the subnode Core Data ServicesData Definitions of the favorite package.
  2. Right-click Data Definitions and choose New Data Definition.

Data Definition Templates

When you create a new data definition, you can begin with a blank page. Alternatively, you can use one of several available templates as a starting point. The recommended template when defining a simple view is Define View Entity.

Usually, a respective dialog appears after you assign the new data definition to your transport request. But you can always return to the template selection later in the editing process.

To choose a template later, go to the Templates tab in the tabstrip below the editor window.

Note

You may define your own templates in the editor preferences. Go to WindowPreferences and search for "Data Definition Templates".

The FROM Clause

A CDS view never stores data by itself. It always references one or more data sources from which it reads the data.

The data foundation of a CDS view is specified in the define statement after keyword FROM. Here you can specify database tables and other CDS views.

Hint

When you press Ctrl + Space to enter the data source using code completion, the editor only suggests development objects that are suitable as data sources.

The Element List

The Element List

Play the video to learn about the element list.

The keyword KEY makes a view element part of the views key. The key elements have to be placed in one block at the beginning of the element list.

For a CDS view, it is not mandatory to define a key but it is strongly recommended. You will see a warning from the syntax check if the view does not contain at least one key element.

There is another warning if the key of the CDS view does not match the key of its data source. There can be reasons to ignore this warning but usually the key of the view should be identical to the key of the data source. There is one general exception though: If the data source is a database table with a client field, this client field should not be part of the element list of the view.

With the AS addition, you can define a name for an element. If an element is just a field of the data source, the AS addition is optional. In that case, the name stays the same as in the data source.

Hint

In database tables, the field names make no distinction between uppercase and lowercase. When a CDS view reads from a database table, it is common practice to increase readability by introducing alias names that consist of a mixture of uppercase and lowercase characters (mixed-case). In the example, employee_id became EmployeeID and first_name became FirstName.

Instead of adding the fields of the data source one by one, you can press Ctrl + Space and choose Insert all elements. This template not only adds all fields of the data source (except for the client field) to the element list, it also takes over the key definition and suggests alias names in mixed-case for all fields.

Note

When you create a CDS view with a DB table or a CDS view as Referenced Object, the Insert all elements template is applied by default.

The DDL Formatter and Comments

The syntax that we use in data definitions is called the CDS Data Definition Language (DDL). To increase readability you can let ADT format the DDL source. Place the cursor somewhere in the data definition and choose Source CodeFormat from the Eclipse menu or press Ctrl + F1.

By default, Eclipse uses a standard formatter profile that it derives from the ABAP system. You can define your own formatter profiles in the Eclipse Preferences dialog. Choose WindowPreferences from the eclipse menu and search for DDL Formatter.

Comments in CDS DDL

Play the video to learn about comments in CDS DDL.

How to Define a Basic CDS View

Play the video to see how to define a basic CDS view.

Define a Basic CDS View

You want to decouple the data access and the table definition. To do so, you define a CDS view entity that read

Template:

  • n.a.

Solution:

  • /LRN/R_EMPLOYEE (Data Definition)

Prerequisites

For this exercise, you need the database table for employee data that you created and filled in a previous exercise (suggested name was: Z##EMPLOY, where ## is your group number). If you have not finished that exercise, create a copy of the database table /LRN/EMPLOY_DEP .

Task 1: Define a CDS View Entity

Define a new CDS view entity that reads from your database table with employee data (suggested name: Z##_R_Employee, where ## is your group number). Use a template that adds all table fields to the element list of the view (except for the client field, the annual salary and the currency code) and that takes over the key definition from the database table.

Steps

  1. In your own package, create a new data definition (suggested name: Z##_R_EMPLOYEE, where ## is your group number). Specify your database table as Referenced Object and choose the Define View Entity template to generate the definition statement, some standard annotations and the element list.

    1. In the Project Explorer view, right-click your database table definition Z##EMPLOY to open the context menu.

    2. From the context menu, choose New Data Definition.

    3. Confirm that the Package field contains the name of your package and that the Referenced Object field contains the name of your database table definition.

    4. In the Name field, enter the name for the CDS view entity (Z##_R_Employee, where ## is your group number).

    5. Enter Employee (Entity) in the Description field and choose Next.

    6. Confirm the transport request and choose Next.

      Caution

      Make sure you don't choose Finish yet. If you do, you are not able to choose the template that you want to use.
    7. From the list of Templates, choose Define View Entity, then choose Finish.

  2. For the time being, comment elements AnnualSalary and CurrencyCode.

    1. Select both code rows and press Ctrl + < to add two slash signs (/) at the beginning of each row.

  3. Apply source code formatting.

    1. From the eclipse menu, choose Source CodeFormat. Alternatively, choose Shift + F1.

  4. Activate the data definition.

    1. Press Ctrl + F3 to activate the development object.

Task 2: Analyze the CDS View Entity

Analyze the technical implementation of the CDS view entity in the database and test the result.

Steps

  1. Display the SQL statement with which the technical representation of the view entity is created in the database.

    1. Right-click anywhere in the source code of the data definition and choose Show SQL CREATE Statement.

  2. Open the Data Preview for your CDS view.

    1. Right-click anywhere in the source code of the data definition and choose Open WithData Preview. Alternatively, place the cursor anywhere in the source code of the data definition and press F8.

Log in to track your progress & complete quizzes