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.
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
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
- CDS Part 1. Core Data Services – Introduction
- CDS Part 2. Core Data Services – Deep Dive
- ABAP on SAP HANA. Part XII. Open SQL, CDS or AMDP, which Code to Data Technique to use?
- CDS Part 3. Expose CDS Views as OData Service through Annotation
- CDS Part 4. Data Access in S/4HANA Cloud – CDS View Introduction
- CDS Part 5. ABAP CDS Views With Authorization
- CDS Part 6. Basic Expressions & Operations Available for CDS View – I
- CDS Part 7. Basic Expressions & Operations Available for CDS View – II
- CDS Part 8. Usage of Built-In Functions in CDS – I
- CDS Part 9. Usage of Built-In Functions in CDS – II
- CDS Part 10. Usage of Built-In Functions in CDS – III
- CDS Part 11. How to Consume CDS View in Smart Business Service KPI Fiori Apps?
- CDS Part 12. Useful 6 CDS Related Tools in ADT
- CDS Part 13. Key Definition in CDS Views
- CDS Part 14. ABAP Annotations for Translatable Texts in CDS Views
- CDS Part 15. Associations in CDS Views – I