6 SAP ABAP CDS Views Best Practices (+ Examples)

Photo of author
|

These are SAP ABAP CDS views best practices.

You’ll learn best practices like:

  • Virtual data model usage
  • Correct naming conventions
  • Annotation usage
  • Many more

If you know of a best practice that’s not listed here, please leave a comment below.

Let’s get started!

#1 Use the Virtual Data Model

Incorporate your SAP ABAP CDS views into an existing virtual data model or create one from scratch if no virtual data model exists, such as in SAP R/3 or SAP ECC greenfield.

The virtual data model is the foundation for both transactional and analytical applications, and APIs (application programming interfaces).

The virtual data model does:

  • Avoid redundant SAP ABAP CDS views
  • Provide a consistent model
  • Helps to maintain an overview
  • Structure the data into logical units from a business perspective
  • Provides consistent names

The virtual data model has four layers:

  • Consumption views
  • Composite interface views
  • Basic interface views
  • Database tables
SAP Virtual Data Model

The ABAP CDS views in a virtual data model should be marked with the annotation @VDM.viewType: #<VDM_CDS_VIEW_TYPE>.

<VDM_CDS_VIEW_TYPE> is either:

  • #BASIC
  • #COMPOSITE
  • #CONSUMPTION

An ABAP CDS view can only be one of the types above.

The virtual data model is valid both for SAP ABAP CDS views and SAP HANA CDS views.

Let’s start at the bottom of the virtual data model with the database tables layer:

Database Tables

The transparent tables of the database such as:

  • MARA
  • VBAK
  • USR01

Basic Interface Views

The annotation for a basic interface view is @VDM.viewType: #BASIC.

Basic interface views are built on top of database tables, but are the bottom of the virtual data model.

Access to the database tables is limited to basic interface views. All other CDS views get their data from the basic interface views.

A basic interface view selects data from a single database table.

A basic interface view doesn’t convert or calculate any database table values.

The purpose of a basic interface view is:

  • Mirror the database table in a CDS view shape
  • Transform the technical names of a database table into human-readable names that are used in the business
  • Transform the technical types of a database table via cast operation to make them more precise
  • Enhance database table fields with additional semantic annotations

Additional semantic annotations in basic interface views should document the database table field semantic.

Composite Interface Views

The annotation for a composite interface view is @VDM.viewType: #COMPOSITE.

Composite views build on top of basic views and other composite views.

Composite interface views are the middle layer of the virtual data model: they’re the bridge between the basic and consumption views.

Composite interface views project data for specific purposes like an application. They usually combine data from different sources. They are reused by other composite views and consumed by consumption views.

Analytical CDS views like fact sheets, cubes, and dimensions are composite interface views.

The purpose of a composite interface view is to:

  • Be reused
  • Project data for a specific purpose
  • Combine data from multiple data sources.

Consumption Views

The annotation for consumption views is @VDM.viewType: #CONSUMPTION.

Consumption views are built on composite interface views. Consumption views are at the top of the virtual data model.

Consumption views are meant to be consumed. For example, by:

  • SAPUI5 and Fiori applications through OData services
  • Transactional service models
  • Analytical queries

Consumption views aren’t meant for general reuse. Rather, they are made to provide the exact data an application or system needs.

Consumption views come in three flavors:

  • Consumption views like transactional consumption views
  • Analytical consumptions views such as a query
  • Remote API consumption views, like defining a web service API

Consumption views can control the layout and behavior of transactional and analytical applications through annotations.

#2 Use Type Annotations for Basic Views, Composite Views, and Consumption Views

Use the respective type annotation for basic, composite, and consumption views in the DDL (Data Definition Language) file:

  • @VDM.viewType: #BASIC
  • @VDM.viewType: #COMPOSITE
  • @VDM.viewType: #CONSUMPTION

SAP uses the @VDM.viewType annotation for internal structuring and interpretation of the CDS views, for example, in the Fiori View Browser.

#3 Use Naming Conventions for CDS views (THE SAP WAY)

A SQL View Name can be up to 16 characters, and a CDS View name can be up to 30 characters.

Use Z, Y, or the company’s registered namespace as the first character to indicate it’s a custom file, not an SAP file.

Use the field names defined by SAP when referring to SAP business entities. When creating your own fields or extending SAP views, use YY or ZZ as the prefix.

This is how SAP names its CDS views. Below is an alternative way to name CDS views (the SAP way isn’t always the best).

SQL View Name

<PREFIX><DESCRIPTION>—for example, ZIBPADDR.

The <PREFIX> consists of:

  1. Y, Z, or the company’s registered namespace
  2. I (interface view—basic & composite views), C (consumption view), A (remote API view), P (private VDM view), E (extension include view), X (VDM view extension)

The <DESCRIPTION> is:

  1. All written together
  2. All in upper case letters
  3. Views that are value helper CDS views end in VH, for example, ZCBPADDRVH. Value helper CDS views view are always a consumption views.
  4. If the view is a transactional object CDS view or a transactional consumption view a TP at the end, for example, ZCFOOTP

CDS View Name

<PREFIX><Description>—for example, ZI_BPNameAddress.

The <PREFIX> consists of

  1. Y, Z or the registered namespace of the company
  2. I (interface view—basic & composite views), C (consumption view), A (remote API view), P (private VDM view), E (extension include view), X (VDM view extension)
  3. Underscore _

The <Description> is:

  1. All written together
  2. In CamelCase
  3. Views that are value helper CDS views end in VH, for example, ZCBPADDRVH. Value helper CDS views view are always a consumption views.
  4. If the view is a transactional object CDS view or a transactional consumption CDS view, there’s a TP at the end, for example, ZC_FooTP.

By the way, here are three ways you can find SAP ABAP CDS view demos and examples.

#5 Use Naming Conventions for CDS Views (NOT THE SAP WAY)

A SQL View Name can be up to 16 characters, and a CDS View name can be up to 30 characters.

Use Z, Y, or the company’s registered namespace as the first character to indicate it’s a custom file, not an SAP file.

Use the field names defined by SAP when referring to SAP business entities. When creating your own fields or extending SAP views, use YY or ZZ as the prefix.

Here’s the non-SAP (and probably more straightforward) way to name your CDS views:

  • Differentiate between basic and composite views
  • Mark a value helper CDS view not with a suffix, but with a prefix
  • Mark transactional CDS views not with a suffix, but with a prefix

At the latest, when you start adding analytic views to your repertoire, you will need to come up with other naming conventions for your CDS views than SAP does:

SQL View Name

<PREFIX><DESCRIPTION>—for example, ZIBPADDR.

The <PREFIX> consists of:

  1. Y, Z or the company’s registered namespace
  2. B (basic view), O (composite view), C (consumption view), A (remote API view), P (private VDM view), E (extension include view), X (VDM view extension)

The <DESCRIPTION> is:

  1. All written together
  2. All in upper case letters
  3. When the CDS view is a value helper CDS view, a V goes right after the C for consumption CDS view, for example, ZCVBPADDR. Value helper views are always consumption views.
  4. If the view is a CDS view, or a consumption view, we need to put T right after the B for basic, or O for composite, or C for consumption view. For example, ZCTFOO.

CDS View Name

<PREFIX><Description>—for example, ZI_BPNameAddress.

The <PREFIX> consists of:

  1. Y, Z or the company’s registered namespace
  2. B (basic view), O (composite view), C (consumption view), A (remote API view), P (private VDM view), E (extension include view), X (VDM view extension)
  3. Underscore _

The <Description> is:

  1. All written together
  2. In CamelCase
  3. If it’s a value helper CDS view, then a V follows the C, for example, ZV_BPName. A value helper CDS view is always a consumption view.
  4. If it’s a transactional view, you put T after the B for basic, O for composite, or C for consumption, like ZCT_Foo.

#6 Use Naming Conventions for Analytical CDS Views (NOT THE SAP WAY)

A SQL View Name can be up to 16 characters, and a CDS View name can be up to 30 characters.

Use Z, Y, or the company’s registered namespace as the first character to indicate it’s a custom file, not an SAP file.

Use the field names defined by SAP when referring to SAP business entities. When creating your own fields or extending SAP views, use YY or ZZ as the prefix.

This is an alternative to SAP’s naming convention to name your analytical CDS views:

SQL View Name

<PREFIX><DESCRIPTION>—for example, ZFBMARA.

The <PREFIX> consists of:

  1. Y, Z or the registered namespace of the company
  2. BF (basic fact view), OF (composite fact view, BD (basic dimension view), OD (composite dimension view), BC (basic cube view), OC (composite cube view), or CQ (consumption query view)

The <DESCRIPTION> is:

  1. All written together
  2. All in upper case letters

CDS View Name

<PREFIX><Description>—for example, ZBF_Mara.

The <PREFIX> consists of:

  1. Y, Z or the company’s registered namespace
  2. BF (basic fact view), OF (composite fact view, BD (basic dimension view), OD (composite dimension view), CB (cube basic view), OC (composite cube view), or CQ (consumption query view)
  3. underscore _

The <Description> is:

  1. All written together
  2. In CamelCase

Add Your SAP ABAP CDS View Best Practice

Do you miss any SAP ABAP CDS view best practice? Then it’s your turn:

Leave your SAP ABAP CDS view best practice in a comment below.

Leave a Comment