Wednesday, 25 October 2017

New Data Modeling Features in SAP NW ABAP 7.4 SP5

The Challenge

With the availability of the SAP HANA platform there has been a paradigm shift in the way business applications are developed at SAP. The rule-of-thumb is simple: Do as much as you can in the database to get the best performance. This is also true for the underlying data models of the business applications.

Until now, data modeling in ABAP typically involved organizing your data in database tables/views and often providing some additional high-level services for the applications using suitable ABAP frameworks. It stands to reason that to enable real-time businesses some of these services should ideally also be brought closer to the data(base).

This presents several challenges. High quality data models should provide a single definition and format for the data. They should be clear and unambiguous, reusable and flexible, even extensible. So how can you capture the semantics of the data model in the database so that the model can be easily reused by different consumers, e.g. by OData clients and by OLAP tools? How can you extend the meta model to service your applications? Impossible, you say?  Maybe, if we didn’t have Core Data Services (CDS).

The Solution

CDS simplifies and harmonizes the way you define and consume your data models, regardless of the consumption technology. Technically, it is an enhancement of SQL which provides you with a data definition language (DDL) for defining semantically rich database tables/views (CDS entities) and user-defined types in the database. The enhancements include:

◉ Annotations to enrich the data models with additional (domain specific) metadata
◉ Associations on a conceptual level, replacing joins with simple path expressions in queries
◉ Expressions used for calculations and queries in the data model

CDS entities and their metadata are extensible and optimally integrated into the ABAP Data Dictionary and the ABAP language.

CDS is supported natively in both the ABAP and the HANA Platforms! In fact, CDS is (in my opinion) the most ambitious and exciting SAP development in the area of data modeling in recent years. You can finally define and consume your data models in the same way (syntax, behaviour, etc.) regardless of the SAP technology platform (ABAP or HANA). Unwantedly the phrase: “One Data Model to rule them all” always comes to mind when I think of CDS.

CDS Support in SAP NW ABAP 7.4 SP5

With SAP NW ABAP 7.4 SP5 the first instalment of CDS support in ABAP has been delivered. This provides you with advanced viewbuilding features which you can use to optimize your data models.

Prerequisite is the ABAP Development Tools for Eclipse (ADT) 2.19 since the new CDS tooling is only available in ABAP in Eclipse.

Let’s take a look at some of these new features ABAP in Eclipse.

Create a new DDL source file

You can create a new DDL source in ABAP in Eclipse via File > New > Other … > ABAP > DDL Source

SAP ABAP 7.4, SAP ABAP SP5, SAP ABAP Certifications, SAP ABAP Material

View definition

Your new DDL source is opened in a text editor in ABAP in Eclipse. Initially the source is empty. Using the DEFINE VIEW statement you can define your CDS view entity.

SAP ABAP 7.4, SAP ABAP SP5, SAP ABAP Certifications, SAP ABAP Material

View entities are defined as selects from one or more datasources. Datasources can be other view entities, database tables or classical DDIC views (SE11 views). The select list ist defined in curly brackets after the from clause (great for code completion!). The elements in the select list are separated by a comma. The code snippet above defines a simple view entity called SalesOrder on the database table SNWD_SO. SNWD_SO contains the sales orders data.

Currently you can only define one CDS entity per DDL source.

Joining data sources

You can combine records from two or more data sources using join clauses. You can also specify aliases for the datasources.

SAP ABAP 7.4, SAP ABAP SP5, SAP ABAP Certifications, SAP ABAP Material

In addition to INNER JOIN you can also model a LEFT OUTER JOIN, RIGHT OUTER JOIN, UNION and/or UNION ALL.

The comparison operators BETWEEN, =, <>, <, >, <=, >=, NOT and LIKE can be used in the on and where clauses. In addition IS NULL and IS NOT NULL are also valid where-conditions.

Aggregations and SQL functions

CDS also provides support for aggregations (SUM, MIN, MAX, AVG, COUNT), SQL functions (LPAD, SUBSTRING, MOD, CEIL, CAST) and CASE statements in view entities.

SAP ABAP 7.4, SAP ABAP SP5, SAP ABAP Certifications, SAP ABAP Material

In the above example the view selects the business partners with outstanding sales orders which together (SUM) amount to more than EUR 100.000 (HAVING SUM). The outstanding amounts are reported per business partner role (GROUP BY). The codes for the business partner roles are translated to readable text in the database (CASE).

Semantically rich data models

Annotations can be used to add metadata to CDS entities. Annotations specify the properties and semantics of the entity and its behaviour when it is accessed at runtime. This metadata can also be accessed by consumption tools using special APIs. In future, consumers will be able to extend existing view definitions and add their own annotations without having to re-model the data (“One Data Model to rule them all“). Annotations always begin with the @ character.

SAP ABAP 7.4, SAP ABAP SP5, SAP ABAP Certifications, SAP ABAP Material

Above the SAP buffering behaviour is specified using the @AbapCatalog.buffering annotation. Here single record buffering is enabled (prerequisite is that the underlying datasource allows buffering). In addition the element currency_code is defined as a currency key. The element gross_amount is defined as a currency field and the currency key currency_code is assigned to the field.

In every view definition the compulsory annotation @AbapCatalog.sqlViewName must be specified. This annotation specifies the name of the corresponding view in the ABAP Dictionary. CDS entities are integrated into the ABAP Dictionary and ABAP language using the same infrastructure which exists for classical Dictionary views. The CDS entity name (here SalesOrder) can be thought of as an alias for the Dictionary View. The metadata which is specified for the CDS entity, however, can only be accessed via the entity name.


The ABAP Dictionary enhancement concept is also supported in ABAP CDS entites. By using $EXTENSION.* in the select list, all fields that are added as enhancements to the underlying database table or classical DDIC view are automatically added to the CDS entity.

SAP ABAP 7.4, SAP ABAP SP5, SAP ABAP Certifications, SAP ABAP Material

Here any fields which are added to the database table SNWD_SO via enhancements, will automatically be added to the view entity. Currently it is only possible to use $EXTENSION.* in view definitions with exactly one datasource (no joins, unions, etc.).

In future, CDS will also provide additional features for extending existing CDS entities themselves (not via the underlying datasource). These features will be available on both the ABAP and HANA platforms.

Consuming your CDS entities in ABAP

Once you have saved and activated your DDL source, the DDIC artifacts are created. Consuming your CDS entity is simple: CDS entities can be used in OPEN SQL! You should always use the entity name in your OPEN SQL statements.

SELECT * FROM SalesOrder INTO TABLE @itab. “Use entity name

Note that when selecting from CDS entities in OPEN SQL, the new OPEN SQL syntax must be used. This means that the ABAP variables (in this case itab) must be escaped using the @ character. The @ character in OPEN SQL has nothing to do with CDS annotations. For more information about the new OPEN SQL syntax.

Lifecycle and database support

The best part about DDL sources and CDS entities is that they are managed by ABAP. This means that the entire lifecyle of the CDS entities are controlled by the ABAP Change and Transport System (CTS).

In addition, the SP5 CDS features are “open”. This means that your CDS view definitions can be deployed on any database which is supported by SAP.

1 comment:

  1. I am interested in knowing if CDS views will eventually replace ABAP "Dictionary Views". I cannot join table CRMD_PARTNER with BUT000 with a CDS view, as it will not handle joining a RAW(16) field with a CHAR(32) field, but I note that I can join these two tables using an ABAP "Dictionary View". I can then join my ABAP Dictionary View up with other tables in an ABAP CDS View. I am concerned about creating ABAP Dictionary views however as to if these are going to continue to be long term supported now that we have ABAP CDS Views and HANA CDS Views.