Skip to Content
Author's profile photo Pavankumar Reddy

Core Data Services in ABAP

Core Data services (CDS) are domain specific languages (DSL) and services for defining and consuming semantically rich data models in SAP HANA. They are integral part of SAP HANA, and can be leveraged in the ABAP stack.

/wp-content/uploads/2016/02/1_892469.png

Core Data Services are enhanced “view entities” in ABAP provides several advantages:

  • Pushes data intensive Calculations into database layer.
  • Queries can be combined,  ex: UNION, UNION ALL
  • View on View and View extensions are supported
  • Association between view entities can be defined.
  • Case expression supported
  • Arithmetic, String and cast expression supported
  • ABAP CDS supports INNER joins, OUTER joins, and RIGHT OUTER joins.
  • Annotations can be used.
  • Aggregations, Grouping, Filtering groups
  • Support for AnyDB  But no access to specific HANA features
  • Supports about 90% of SQL features
  • Supports ABAP and SAP HANA deployment.

 

Support for CDS in ABAP begins with ABAP 7.4 SP2. Especially with SAP NetWeaver ABAP 7.40 SP5 additional capabilities like CDS Associations are supported. CDS Associations can be used to replace joins with simple path expression in queries.

 

The goal of ABAP CDS is to fully support the SQL-92 standard and beyond that to support additional features likes associations, entities, create, read, update, and delete (CRUD) and OData Services.

 

/wp-content/uploads/2016/02/1_892469.png

ABAP 7.4 SP05 support code to data paradigm. ABAP stack manages all entities and deploys all artifacts from ABAP layer into the database layer. ABAP server act as master for developing, managing and calling HANA views.  On Activation of CDS views two objects are generated one CDS Database View and other CDS entity.  CDS views are defined for existing database tables, any other views or CDS views in ABAP dictionary. Syntax for defining CDS view is similar to SQL.

 

Simple CDS view:

/wp-content/uploads/2016/02/1_892469.png

To create core data services ABAP Development Tools for SAP Netweaver is used. ABAP workbench does not support the new type of ABAP repository objects called “DDL Sources”. DDL statement DEFINE VIEW and SQL like syntax is used to define the view. Annotations marked with @ sign provides additional metadata such as how to buffer view records, SQL view name, client dependency and reference between quantity and amount columns and the corresponding currency and unit code columns.

 

The annotation @AbapCatalog.sqlViewName is mandatory, and specifies the  name of DDL source in the ABAP Dictionary and the database.  On Activation of DDL source two objects are created CDS database view and CDS entity. CDS database view can be accessed using SE11 ABAP dictionary. These representation are generated, updated, and deleted automatically when activating DDL source.

 

In the above example simple CDS view, “ZCDS_DDL_SAMPLE2” is CDS ABAP Database View and “ZCDS_SAMPLE2” is CDS entity. CDS entity can be identified using “DEFINE VIEW” syntax and CDS ABAP Database view can be identified using “@AbapCatalog.sqlViewName” annotation which on activation accessible from SE11- ABAP Dictionary View. Same applies to all the examples shown below.

 

CDS View with column list and using column alias:

/wp-content/uploads/2016/02/1_892469.png

Here CDS view with column list with curly brackets and using column alias to rename a column is shown. Column list are comma separated and alias names are given with keyword ‘AS’.

 

Case Expressions in CDS View:

/wp-content/uploads/2016/02/1_892469.png

In the new open SQL, ABAP CDS supports the CASE expression.  It is also possible to nest cast expression. An Alias name is required for the resulting column except for nested CASE. The resulting column type is derived from expressions after THEN and ELSE clause.

 

String, Arithmetic and Cast expressions in CDS Views:

/wp-content/uploads/2016/02/1_892469.png

Arithmetic expressions using operators +, – , *, & or the unary -, or using built in functions such as CEIL( ) and MOD( ) are used. String expressions, including string concatenation operator &&, or built in functions such as SUBSTRING( ) are used. Cast expressions are also supported.

 

Joins in CDS Views:

/wp-content/uploads/2016/02/1_892469.png

ABAP CDS support INNER joins, LEFT OUTER joins and RIGHT outer joins. Join conditions can be inserted before or after the select list. Brackets are used to implement complex join operators. When joins are used asterix is not supported in SELECT statements.

 

Annotations in CDS Views:

/wp-content/uploads/2016/02/1_892469.png

Annotations specify additional information such as how records of the view should be buffered by ABAP table buffer, whether view is client dependent or not. By default CDS views are client dependent and there is no need to include client fields explicitly in the field list or in the join conditions.

 

Annotations are marked with the @sign on top of DDL source code in the view. In above example @AbapCatlog.Buffering.status states whether buffering is switched off or active, @AbapCatlog.Buffering.type states type of buffering for CDS view, @Semantics is used for reference currency and quantity fields.

 

Aggregation, Grouping, Filtering Groups in CDS Views:

/wp-content/uploads/2016/02/1_892469.png

Through ABAP CDS calculation of aggregations, grouping records and filtering groups are supported. GROUP BY and HAVING are used.

/wp-content/uploads/2016/02/1_892469.png

Simple and complex filtering conditions are supported. It is also possible to include WHERE conditions in CDS view definitions.

 

Unions in CDS Views:

/wp-content/uploads/2016/02/1_892469.png

Using the UNION and UNION ALL keywords views are merged from two or more queries. Here UNION implies DISTINCT, the combined result contains no duplicate rows, UNION ALL implies the combined result has duplicate rows.

 

Selecting Data Using CDS Views:

/wp-content/uploads/2016/02/1_892469.png

/wp-content/uploads/2016/02/1_892469.png

The open SQL SELECT statement is used in ABAP to select data from a CDS view. ABAP Dictionary View name, respectively SQL view name of the CDS view is used in select from clause.

 

When using the new Open SQL syntax you can used CDS View name which is identified using DEFINE VIEW keyword in the select from clause. Note that CDS view name cannot have the same name as the SQL view/ ABAP Dictionary view and the CDS view name also does not have same name of the ABAP Dictionary.

 

In the above new Open SQL syntax CDS view is used in select from clause, you might notice the “@” symbol used fro escaping of host variables. Host variable basically mean an ABAP variable or constant inside Open SQL statement. Every host variable except literals can, should, and must be escaped with the preceding “@” sign at every position in the statement. If one host variables escaped all host variables have to be escaped.

 

To retrieve the data from a client dependent CDS view, new Open SQL syntax USING CLIENT is used.

 

Hope you find this blog helpful.

 

In my next blogs I will try to touch how to create CDS view, different ways to expose CDS views as OData Service and Associations in CDS views.

Expose CDS Views as OData Service

CDS Table function feature

Step by Step Hierarchies in S/4 HANA Analytics

Define the Analytical Query CDS View

Your suggestions, feedback, comments on this blog are most welcome.

Note: This is my previous SCN for the new and latest blogs please follow my new SCN blog https://people.sap.com/reddypavankumar

Assigned Tags

      21 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      nice article.. thanks

      Author's profile photo Shruthi Padmanabhan
      Shruthi Padmanabhan

      very helpful.. thanks

      Author's profile photo Former Member
      Former Member

      Article was very helpful to understand CDS views.

      Author's profile photo Former Member
      Former Member

      good article.. please post more...

      Author's profile photo Pavankumar Reddy
      Pavankumar Reddy
      Blog Post Author

      Thanks!!

      Author's profile photo Kiran Padarthi
      Kiran Padarthi

      Nice article.... Thanks

      Author's profile photo Vanka Venkat
      Vanka Venkat

      very helpful..thank you

      Author's profile photo Former Member
      Former Member

      Thanks for your review Venka

      Author's profile photo Former Member
      Former Member

      nice article and nice info .. very helpful !!

      Author's profile photo Former Member
      Former Member

      Nice blog..keep posting further ..it is very helpful..Thank you

      Author's profile photo Anusha D
      Anusha D

      Very nice blog.. helpful!!!

      Author's profile photo Former Member
      Former Member

      Nice blog and easy to learn

       

      Hope to see more new topics very soon.

      Author's profile photo Former Member
      Former Member

      Helpful, but how abap cds support SQL nested view, e.g. select * from (select * from tab B)?

       

      Thanks,

      Anni

      Author's profile photo Avinash D M
      Avinash D M

      Very use full article beginners.Thanks

      Author's profile photo Petr Benes
      Petr Benes

      Very nice stuff. Just one comment to using the SO_ID in GROUPING BY. Using SO_ID it's probably no good idea as SO_ID contains IDs. IDs are usually unique and therefore the GROUPING BY won't work. Even if you define it like "substring(so_id, 1, 2) as id" the GROUPING BY will still use the whole SO_ID and not only the the first 2 characters.
      But I think you maybe did it for purpose, because it made me really hard times and I learned even more

      Author's profile photo Naresh Bollepogu
      Naresh Bollepogu

      Nice blog!!.

      any idea how to output extra columns from union? i'm trying like null as XX but error says unexpected word null.

      br,

      nb

       

      Author's profile photo Saswata Chandra
      Saswata Chandra

      Extremely useful article. Since we have an array of annotations in CDS and those have robust functionalities attached with them, anything on that could be a great help.

      Author's profile photo Maddirevula N Sathish Kumar Reddy
      Maddirevula N Sathish Kumar Reddy

      Hi Pavan,

      Can you send me the exact difference between Basic and Composite view...

      Author's profile photo Sakae Mizuka
      Sakae Mizuka

      When I try to create a CDS View as below to extract data to BW, it tells that detectDeletedRecords is not supported, do you know why and are you facing the same issue?

      --------------------------------------------------------------------------

      @AbapCatalog.sqlViewName: 'ZCDS_V_DSV01'
      @AbapCatalog.compiler.compareFilter: true
      @AccessControl.authorizationCheck: #CHECK
      @EndUserText.label: 'ZCDS_V_DS03'
      @Analytics: { dataCategory: #CUBE,
      dataExtraction: { enabled :true,
      delta.byElement: {
      name : 'lastChangedAt',
      maxDelayInSeconds : '100',
      detectDeletedRecords: true,
      ignoreDeletionAfterDays: 365
      }
      }}

      define view ZCDS_V_DS03 as select from rsbkdtp {
      key rsbkdtp.dtp,
      key rsbkdtp.objvers,
      rsbkdtp.tgt,
      rsbkdtp.tstpnm,
      @Semantics.systemDate.lastChangedAt
      rsbkdtp.timestmp as lastChangedAt

      }


      Regards

      Shidong

      Author's profile photo Pavankumar Reddy
      Pavankumar Reddy
      Blog Post Author

      Hi,

      I am not facing any issue. May be you are using lower version that is why you are getting error.

      Regards,

      Pavan

      Author's profile photo Sachin Patil
      Sachin Patil

      Hello Expert,

      When I try to create a CDS View as below to extract data to BW, it tells that detectDeletedRecords is not supported, do you know why and are you facing the same issue?

      @Analytics.dataExtraction.delta.byElement.name: 'LastChangeDateTime'
      @Analytics.dataExtraction.delta.byElement.maxDelayInSeconds: 1800
      @Analytics.dataExtraction.delta.byElement.detectDeletedRecords: true
      define view Zds_I_Salesdocument as select from I_SalesDocument {

      Can you please help us what is wrong and why it is not supported. The SAP version is SAP S4 HANA 1610 and HANA client version is

      SAP HANA Studio

      Version: 2.3.15