Standard service - a fixed amount of work that is performed at a fixed price.  Post Service

  

Thursday, 14 February 2019 15:59

CDS Part 13. Key Definition in CDS Views

Written by 
Rate this item
(0 votes)

If you are working in S/4HANA projects, Core Data Service (CDS) should be your buddy. Of all the innovations in SAP, Core Data Service is one of the

smartest. CDS overcomes the transport cumbersomeness of Information Models (HANA Views viz Attribute, Analytical, Calculation). Information Models need to be saved in Packages and then the Packages are saved in the Delivery Units. Delivery Units have to be exported from one system and imported in another system. If you have corresponding View Proxies created for the HANA Information models (views) to consume in the ABAP layer, then you need to take special care that Delivery Units are exported and imported first and then the View Proxy transport is moved.

HANA Transport Container (HTC) somehow reduced the risk of moving Delivery Units and View Proxy inconsistently. HTC is an object in ABAP Layer which can store Deliver Units of Information Models from Database. And a transport can store both HTC and View Proxy together. This way, two dependent objects can be moved together. But, the developers and the modelers still have the responsibility to save their work in the same transport.

Also, if there is any delta change in the InformationModel in Database, the developer needs to synchronizeand save the change in the HTC and the View Proxy in a common transport(although they can move independently).

All these complexities and dependencies are simplified with the introduction of Core Data Services. We have already shared many tutorials on CDS in our series, but still we believe, there are many more concepts in Core Data Services which we need to explore continuously.

Also Read : ADT Eclipse/HANA Studio for ABAPers

Those of you who are familiar with CDS views would knowabout keyword KEY. In this post Ihave tried to answer some questions which you might have pondered about whileusing this keyword.

  • What is the actual purpose of using KEY inCDS entity?
  • How does it impact the CDS entity?
  • Does it affect the records fetched by CDSview through Open SQL?
  • What is the role of annotation AbapCatalog.preservekey with respect toKEY?

Let’sus try to raise the curtains gradually, one by one.

KEY isused to define the current element as a key element of the current CDS entity.Any elements of a SELECT list can be defined as key elements that are placedwithout gaps at the start of the list.

It will give syntax error “Key must be contiguous and start at the first position” if keyelements are placed with gaps or not at the start of the list

The key elements of the CDS entity are evaluated asfollows if they are used as data source of a SELECT statement in Open SQL:

  • By the addition ORDER BY PRIMARY KEY in select statement. In this case, the keyelements must be defined at the start of the SELECT list of CDS entity withoutany gaps. If key is NOT mentioned, thesystem will derive the key fields implicitly from the key fields of the basistables and the join conditions.
  • In implicit access control

By default, the key elements of the CDS entity are used to document the semantics of the data model. By default, when CDS view is activated and when it is consumed by programs, the addition KEY is ignored.

Also Check How I created my first OData Service in SAP?

This default setting can be overridden by using the view annotation AbapCatalog.preservekey.

Let’s take an example to understand this concept.

Case 1

If the annotation is NOT specified or specified with the value FALSE, the key elements defined using KEY statement in CDS entity are ignored by the SQL Database view. The key fields of the database view, like in classic views, will be derived implicitly from the key fields of the basis tables and the join conditions. If this is not possible, all the fields of the database view are key fields.

For the above CDS entity, we haven’t specified any keys/ annotationAbapCatalog.preservekey.By default, the SQL database view will derive keys from basistables SCARR and SPFLI and the join conditions.

Now let us explicitly define KEY fields in the CDS entitywithout defining the annotation AbapCatalog.preservekey and check the databaseview again.

We can see there is no change in the key fields of the databaseview.

Also Explore : How to Consume Custom OData in SAPUI5 Application

Case 2

If the value TRUE is specified for the annotation AbapCatalog.preservekey, the key elements defined using the KEY are also used as keys of the SQL database view.

We can now see all the key fields defined using KEY inCDS entity are also keys of the database view.

Note – There will be no change in the number of records fetched from the database tables in case of changing the keys of CDS entity.

Output with CDS entity keys as CARRID, CONNID andCARRNAME

Output with CDS Entity key as CARRID only-

As you can see there is no change in the number ofentries in view. The logical reason being, CDS is just a view and it displaysall unique entries comparing all fields which are pulled.

Core Data Service is one of the most interesting topicsin SAP ABAP for HANA. We would like to hear if you have used it for anyinnovative solution.

Please feel free to write your experience in the comments section below.

We have a very active Telegram (App) SAP Technical Group with more than 3780+ SAP Technical Practitioners from 6 Continents of the SAP World. Please join it using below link.
Telegram SAP Technical Discuss Group. You need to install the Telegram App first on your mobile device. Once you have it on your mobile, you can join the group and also access it from the Web on your computer and laptop.

Free Step by Step Core Data Services Exercises

Read 210 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.