With CDS, data models are defined and consumed on the database rather than on the server
1.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.
2. Whenever a CDS view is created and activated, these 2 objects gets generated but with new define View option no DDIC objects are created in the database
- 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
Annotations
An annotation enriches a definition in the ABAP CDS with metadata. It can be specified for specific scopes of a CDS object, namely specific places in a piece of CDS source code
Some important annotations
- @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.lso, the CDS view name and the SQL view name can NOT be same
2. @AbapCatalog.compiler.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.
3. @AbapCatalog.preserveKey: true
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.
4. @AccessControl.authorizationCheck: #NOT_REQUIRED
It is used for authorization purpose
Some Important Points
1.We can also do mathematical calcualtions in CDS Views where we can write the total number of students absent in the class as below
( Total_Strength – Total_present ) as Total_Absent
2.you can also use session variable to use variables like sy-datum for current Date, sy-uzeit for current Time etc.
3.Once you have defined the CDS views you can use it directly in your ABAP Programs like doing a select on the CDS views to get the required data
4. You also have an option to extend the CDS views by using the extend view option in from the context menu in ABAP Development Kit or Eclipse
5. You can also use parameters in CDS views to take user input and filter out the data from the CDS view accordingly
ASSOCIATIONS
If you have created a CDS view with JOINS on 5 different tables then this JOIN conditions will be executed every time this CDS view is triggered. Even though the business user is looking at only fields from 2 tables but the CDS view will first run the Join conditions of all 5 tables and it doesn’t matter how many fields user is looking at. To overcome this problem Associations was developed. With Associations, data will be fetched only when user want to see it
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. Associations are defined with ‘Cardinality’. There are 4 types of Cardinality possible based on the data and relationship in the tables joined;
- 0..1
- 0..n or 0..*
- 1..0
- 1..n or 1..*
Once you have created an association and if you want to make it public expose the association so that system will not create any join beforehand but do it on a need basis
to publish a CDS view with OData Service 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
Composition
The relationship between two objects is termed as Association.
In particular when one object owns another object, then that association is called as Composition, but when one object uses another object then that association is called as Aggregation.
Sales Order Header and Items are associated. So when SO header is deleted then SO items are also deleted. So it’s a composition.
Similarly SO Header and Partners are associated. But when a SO header is deleted then the partners are not deleted. So it is an aggregation.
A CDS composition is a specialized/more strict form of CDS association which defines the CDS entity as the parent entity of the composition target. The composition target entity is the child entity and it must define a TO-PARENT association to its parent.
CDS Sample Programs
- With basic view creation and abap program Click Here.