[SAP CAP PART-02] – CORE DATA SERVICE FUNDAMENTAL

Hi guys, next for before blog about SAP CAP, today I will start fundamental about CDS – Core data service.

CDS is an enhancement to standard SQL technology which runs within ABAP layer which means that the design time objects are created in the ABAP layer and can be transported by regular Transport mechanism with a TR number assigned to each object.  The Design Time object is created in database i.e. the HANA DB which enables Code-to-Data shift.

CDS is an enhancement to standard SQL language, it has all SQL features as below:

  • DDL – Data Definition Language.  Used to CREATE Table, MODIFY Table etc.
  • DQL – Data Query Language.  Used to READ data
  • DCL – Data Control Language.  Used to configure ‘SECURITY’
  • Expression Language – Mathematical calculations, conditions Case..Endcase etc

OK, let’s go. 🙂

SETUP ADT – ABAP Development Tool – ECLIPSE

When working with CDS, We need to work with ADT – ABAP Development Tool and we will use Eclipse.

Go to here to download eclipse

https://tools.hana.ondemand.com/latest

Create a Basic CDS view

As I am thinking, when I start a new things, I often start with simple example. And from there, I will explore deep more and more detaill. And in this blog, It will have no exception. We will start with very simple example as well.

Whenver a CDS view is created and activated, these 2 objects gets generated.  As I mentioned in the introduction, this enables Code-to-Data paradigm shift.

  • DDIC SQL view – It is a Design Time Object and can be seen in tcode- SE11
  • HANA View – It is a run time object and gets created in HANA DB

Put the name ZCDS_BASIC_VIEW

@AbapCatalog.sqlViewName: ‘sql_view_name’

Within first annotation, provide the SQL view name.  This is the DDIC SQL view which gets generated once the CDS view is activated and can be seen in tcode SE11

@AbapCatalog.cpmpiler.compareFilter:true

This annotation defines the behavior of the filtering the data i.e. this first compare the filter conditions and if they match then only the data is fetched.  If the CDS view has join conditions, they are executed only after the filter conditions are matched. In DDIC views data if first fetched and then filtered.

@AbapCatalog.preserveKey: true

Another important annotation of CDS views.  As you know all the DB tables in SAP do have Keys defined and the CDS Views are always created on top of those DB tables.  Now any DB table can have multiple keys defined and you might not want those keys to be the key fields of your view.  So if you set this annotation as true, the only fields you define as Key Fields within your CDS view by adding word ‘Key’ in front of those fields will be the Key fields for the CDS view and the DDIC SQL view which gets generated.

If you set this as ‘False’ the DB table key fields will be defined as Key fields for the views as well, regardless of you adding word ‘Key’ in front of fewer fields.

@AccessControl.authorizationCheck: #NOT_REQUIRED

This annotation is used to add ‘Security’ piece to the CDS view.  When CDS view is ready to be rolled out to a bigger audience within or outside the organization, it is very important to restrict the data based on the authority.  We will look into creation of authorization objects in the later part.  For now let’s keep it #NOT_REQUIRED

@EndUserText.label: ‘CDS View type #BASIC’

This annotation is used to apply business labels other than labels attached in the DB tables, to the fields within the CDS view.  This annotation will override the DB table business label and provides what you specify with this annotation.

OK, next we will get data from table sflight

Active CDS view and check on SE11 on S/4 HANA

CDS View with Expressions and Session Variables

In this session, We will create simple CDS view with an Expression to calculate the available seat in table SFLIGHT

Open CDS view, add code as below

This is result

We can use WHERE clause to filter data. In here we use $session to get variable in system

CDS view Extension

Although SAP created many CDS view standard align with every function, but when implementation project, we need to create custom view. In this section, we will discuss about custom view.

SAP provides multiple standard CDS view related to each functional module and also during a project implementation you will create tons of CDS custom views.  Since the CDS views are just virtual data models, you will want to reuse the view created in one project or for one object to another project/object.  Let’s assume you created a CDS view with 5 fields on TABLE A and for another scenario in a different project you need 8 fields from the same TABLE A.  So instead of creating new CDS view from scratch, we can use the concept of CDS View extension and re-use the previous view we created and extend it with 3 new fields.

Create CDS with template extend view

Example we want to create new CDS view include all field of CDS view which created before ( in this is ZCDS_BASIC_VIEW) and add more field. In this case, we will use template extend.

Ctrl + SPACE to get more field base on CDS view which we created before.

Execute we will have

CDS Views – Joins and Associations

In this section, we need to join many table or view We will use Joins and Associations in CDS View.

We have difference type of JOIN:

  • Inner Join
  • Left Join
  • Right Join

Create CDS with JOIN template

We will create CDS view join two table SFLIGHT and SPFLI to get information of airport base on key carrid

F8 to execute and we have result

With JOIN, everytime front end call to CDS view with JOIN many table, JOIN conditions will executed. This is not good for performance of system. Example, Front end just wants to get 2 field in CDS view with JOIN 5 table, and when front end call to CDS view, JOIN condition will be triggered to get data at many table although front just want to get data at one table

To overcome this problem, SAP did an enhancement to this SQL way of getting data and beautifully developed the concept of ‘ASSOCIATIONS’. With Associations, data will be fetched only when user want to see it. Let’s see how it works

ASSOCIATIONS

ASSOCIATIONS are kind of Joins to fetch data from multiple tables on Join Conditions but these are ‘JOINS ON-DEMAND’ i.e. they will only be triggered when user would access the required data which needs the Association of tables.  For example, your CDS view has 4 Associations configured and user is fetching data for only 2 tables, the ASSOICATION on other 2 tables will not be triggered and the system would return the results quickly, so it enables really high turn-around time as compared to regular SQL JOINS.

Associations are defined with ‘Cardinality’. Syntax : association[<cardinality>]

Cardinality concept is not new and holds the same concept with CDS views as well.  There are 4 types of Cardinality possible based on the data and relationship in the tables joined:

  • 0…1
  • 0…N
  • 1…0
  • 1…N

Create CDS view with ASSOCIATION concept

  • Below is the default structure you will get. As you can see the association cardinality is defined as ‘Association[1]’. You can keep it as it is, if you are not sure what association you have to assign.
  • Check the default Association name as _association_name. This can be any name of your choice but SAP has created a naming convention standard to start this name with an underscore(_) and it is always good to follow SAP standard best practices.
@AbapCatalog.sqlViewName: 'ZSQL_VIEW_ASSTN'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS view with association concept'
define view ZCDS_VIEW_ASSOCIATIONS as select from sflight as sf
association [1] to spfli as _flight
    on sf.carrid = _flight.carrid {
    
    key sf.carrid,
    sf.connid,
    sf.fldate,
    sf.price,
    sf.seatsocc_b,
    sf.seatsmax_f,
    sf.seatsocc_f,
    _flight // Make association public
}

F8 to execute

If we want view data association, right click on row and choose Follow Association

Publish CDS View to ODATA Service

It is very easy to publish a CDS view with OData Service.  To do it we just have to use one annotation @OData.publish: true which will further create an OData Service and we must register it within SAP system via GUI interface.  Once registered, CDS view is ready to be consumed in any application within or outside SAP environment.

To get name of CDS public

Go to T-Code: /n/IWFND/MAINT_SERVICE

Add Service

After add service, we have result

View data

SUMMARY

In this blog, I shared very simple example about How to create CDS view, extend CDS view, JOIN and ASSOCIATION concept in CDS view. Some case, we will receive request from user create CDS view to get some data from DB HANA, get some data from standard CDS view. Next blog we will discuss about Data Modelling and Report. Thanks for your reading and any advise kindly leave your comment on this.

Thanks.

JOSEPH.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.