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.

Log in to track your progress & complete quizzes