Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member205436
Participant

The Challenge

With the availability of the SAP HANA platform there has been a paradigm shift in the way business applications are developed at SAP. The rule-of-thumb is simple: Do as much as you can in the database to get the best performance. This is also true for the underlying data models of the business applications.

Until now, data modeling in ABAP typically involved organizing your data in database tables/views and often providing some additional high-level services for the applications using suitable ABAP frameworks. It stands to reason that to enable real-time businesses some of these services should ideally also be brought closer to the data(base).

This presents several challenges. High quality data models should provide a single definition and format for the data. They should be clear and unambiguous, reusable and flexible, even extensible. So how can you capture the semantics of the data model in the database so that the model can be easily reused by different consumers, e.g. by OData clients and by OLAP tools? How can you extend the meta model to service your applications? Impossible, you say?  Maybe, if we didn't have Core Data Services (CDS).

The Solution

CDS simplifies and harmonizes the way you define and consume your data models, regardless of the consumption technology. Technically, it is an enhancement of SQL which provides you with a data definition language (DDL) for defining semantically rich database tables/views (CDS entities) and user-defined types in the database. The enhancements include:

  • Annotations to enrich the data models with additional (domain specific) metadata
  • Associations on a conceptual level, replacing joins with simple path expressions in queries
  • Expressions used for calculations and queries in the data model

CDS entities and their metadata are extensible and optimally integrated into the ABAP Data Dictionary and the ABAP language.

CDS is supported natively in both the ABAP and the HANA Platforms! In fact, CDS is (in my opinion) the most ambitious and exciting SAP development in the area of data modeling in recent years. You can finally define and consume your data models in the same way (syntax, behaviour, etc.) regardless of the SAP technology platform (ABAP or HANA). Unwantedly the phrase: "One Data Model to rule them all" always comes to mind when I think of CDS.

CDS Support in SAP NW ABAP 7.4 SP5

With SAP NW ABAP 7.4 SP5 the first instalment of CDS support in ABAP has been delivered. This provides you with advanced viewbuilding features which you can use to optimize your data models.

Prerequisite is the ABAP Development Tools for Eclipse (ADT) 2.19 since the new CDS tooling is only available in ABAP in Eclipse.

Let's take a look at some of these new features ABAP in Eclipse.

Create a new DDL source file

You can create a new DDL source in ABAP in Eclipse via File > New > Other ... > ABAP > DDL Source

View definition

Your new DDL source is opened in a text editor in ABAP in Eclipse. Initially the source is empty. Using the DEFINE VIEW statement you can define your CDS view entity.

View entities are defined as selects from one or more datasources. Datasources can be other view entities, database tables or classical DDIC views (SE11 views). The select list ist defined in curly brackets after the from clause (great for code completion!). The elements in the select list are separated by a comma. The code snippet above defines a simple view entity called SalesOrder on the database table SNWD_SO. SNWD_SO contains the sales orders data.

Currently you can only define one CDS entity per DDL source.

Joining data sources

You can combine records from two or more data sources using join clauses. You can also specify aliases for the datasources.

In addition to INNER JOIN you can also model a LEFT OUTER JOIN, RIGHT OUTER JOIN, UNION and/or UNION ALL.

The comparison operators BETWEEN, =, <>, <, >, <=, >=, NOT and LIKE can be used in the on and where clauses. In addition IS NULL and IS NOT NULL are also valid where-conditions.

Aggregations and SQL functions

CDS also provides support for aggregations (SUM, MIN, MAX, AVG, COUNT), SQL functions (LPAD, SUBSTRING, MOD, CEIL, CAST) and CASE statements in view entities.

In the above example the view selects the business partners with outstanding sales orders which together (SUM) amount to more than EUR 100.000 (HAVING SUM). The outstanding amounts are reported per business partner role (GROUP BY). The codes for the business partner roles are translated to readable text in the database (CASE).

Semantically rich data models

Annotations can be used to add metadata to CDS entities. Annotations specify the properties and semantics of the entity and its behaviour when it is accessed at runtime. This metadata can also be accessed by consumption tools using special APIs. In future, consumers will be able to extend existing view definitions and add their own annotations without having to re-model the data ("One Data Model to rule them all"). Annotations always begin with the @ character.

Above the SAP buffering behaviour is specified using the @AbapCatalog.buffering annotation. Here single record buffering is enabled (prerequisite is that the underlying datasource allows buffering). In addition the element currency_code is defined as a currency key. The element gross_amount is defined as a currency field and the currency key currency_code is assigned to the field.

In every view definition the compulsory annotation @AbapCatalog.sqlViewName must be specified. This annotation specifies the name of the corresponding view in the ABAP Dictionary. CDS entities are integrated into the ABAP Dictionary and ABAP language using the same infrastructure which exists for classical Dictionary views. The CDS entity name (here SalesOrder) can be thought of as an alias for the Dictionary View. The metadata which is specified for the CDS entity, however, can only be accessed via the entity name.

Further information about the supported predefined annotations can be found in our CDS keyword documentation.

Extensibility

The ABAP Dictionary enhancement concept is also supported in ABAP CDS entites. By using $EXTENSION.* in the select list, all fields that are added as enhancements to the underlying database table or classical DDIC view are automatically added to the CDS entity.

Here any fields which are added to the database table SNWD_SO via enhancements, will automatically be added to the view entity. Currently it is only possible to use $EXTENSION.* in view definitions with exactly one datasource (no joins, unions, etc.).


In future, CDS will also provide additional features for extending existing CDS entities themselves (not via the underlying datasource). These features will be available on both the ABAP and HANA platforms.

Consuming your CDS entities in ABAP

Once you have saved and activated your DDL source, the DDIC artifacts are created. Consuming your CDS entity is simple: CDS entities can be used in OPEN SQL! You should always use the entity name in your OPEN SQL statements.

SELECT * FROM SalesOrder INTO TABLE @itab. "Use entity name

Note that when selecting from CDS entities in OPEN SQL, the new OPEN SQL syntax must be used. This means that the ABAP variables (in this case itab) must be escaped using the @ character. The @ character in OPEN SQL has nothing to do with CDS annotations. For more information about the new OPEN SQL syntax, see the ABAP keyword documentation


Lifecycle and database support


The best part about DDL sources and CDS entities is that they are managed by ABAP. This means that the entire lifecyle of the CDS entities are controlled by the ABAP Change and Transport System (CTS).

In addition, the SP5 CDS features are "open". This means that your CDS view definitions can be deployed on any database which is supported by SAP.

Summary

Well, that was an attempt to give you a condensed overview of Core Data Services (CDS) in SAP NW ABAP 7.4 SP5. Not easy when you are introducing the next game changer in SAP application development.

To sum it up: CDS provides enhanced view building capabilities to enable you to easily define semantically rich and re-useable data models in the database. The new view building features include new join types, new clauses as well as support for aggregate functions and SQL functions. All these features are "open". The features are provided in a new text editor for DDL sources in ADT 2.19.

But the journey doesn't end here. There will continue to be future CDS feature deliveries in ABAP and even in 7.4 SP5 there is still a lot to tell. We haven't even touched on associations or the cast functionality, not to mention the cool editor features. But that will have to wait for another time (otherwise nobody will read this lengthly blog).

If you can't wait you can get more information by watching Carine's video tutorial ("Building Core Data Services Views in ABAP on SAP HANA"). Or you can check out our CDS keyword documentation. For more information about the CDS support in the HANA Platform, see the SAP HANA Developer Guide.




80 Comments
former_member185171
Participant
0 Kudos

We just got this interesting CDS and some questions occur.

@AbapCatalog.sqlViewName: 'sql_view_name'
@EndUserText.label: 'Mijn eerste probeersel met CDS'
define view Zs_Cds_Number_One as select from hrp1000 {

key hrp1000.otype,

        hrp1000.objid
    }

Is there a way to 'synchronise' both names, to make sure they are equal? Is a Code Inspector check possible on these views e.g.

Regards.

Kris

former_member205436
Participant
0 Kudos

Hi Kris,

this is not possible. The sqlViewName and the entity name (here 'Zs_Cds_Number_One') share the same namespace. This means that you have to specify different names otherwise you will get syntax errors.

The sqlViewName also has the same restrictions as the view names in the SE11. The sqlViewName can be max. 16 characters long. The entity name, however, can be 30 characters long. This extension allows you to implement better names for your CDS entities. Names which better convey the semantics. Keeping the names the same (even if it was possible) would mean that you would have to apply the same restrictions to the entity name. You'd be "wasting" the additional characters.

The entity name can, however, have the same name as the DDL source name. We don't check this since the CDS specification allows for multiple entities to be implemented in the DDL source. This has not yet been implemented in ABAP, but when/if it is, then you probably don't want the DDL name to be the same as one entity in your source. You would probably prefer to choose a source name which describes the semantics of all the included entities.

Cheers

Chris

anita_dixit
Employee
Employee
0 Kudos

Hi experts,

I created a simple CDS view with one input parameter and use it for 2 places in the "where" condition, But I am getting the errors as shown :

can anyone please help here?

Thanks

Former Member
0 Kudos

so r u doing bw-hana platform,....tell me

anita_dixit
Employee
Employee
0 Kudos

Yes. I checked and it is not supported as of now. Thanks.

anita_dixit
Employee
Employee
0 Kudos

Hi,

I tried using "having " in one of ABAP CDS views but it is not working.

Here, I need to retrieve top 5 employee with max total revenue, how to do that?

Can anyone help here please ?

************************************

define view Zsoemp as select from snwd_so as so1

association [1] to snwd_employees as emp

   on so1.created_by = emp.node_key

{

    

     key emp.employee_id as emp_id,

    key so1.so_id as so_id,

    emp.node_key,

    emp.first_name,

  @Semantics.currencyCode: true

    emp.currency,

   @DefaultAggregation: #SUM 

   sum(so1.gross_amount) as Total_Revenue,

   @DefaultAggregation: #SUM

   sum(so1.net_amount) as Total_Net,

   @DefaultAggregation: #SUM

   sum(so1.tax_amount) as Total_Tax

}group by emp.employee_id,so1.so_id,emp.node_key,emp.first_name,emp.currency

having count(distinct emp.employee_id) = 5

**************************************

thanks

Former Member
0 Kudos

i can help you, if you tell me exactly on which platform you are working....and which tool you are using i mean version ...

anita_dixit
Employee
Employee
0 Kudos

I am working on BW on HANA . SAP_BW - Release 750

and using Eclipse IDE for Java Developers

Version: Kepler Service Release 2

Build id: 20140224-0627

Thanks

Former Member
0 Kudos

that is amezing ..u got opportunity with latest BW awesom,

so now exactly state ur error ...

anita_dixit
Employee
Employee
0 Kudos

There is such no error, I need to know how to select the top 5 (which has the highest number of sales).

We can't put "select top 5 ..." in ABAP CDS views, so what is the alternative one?

Regards,

Anita

0 Kudos

Hi Anita,

currently the ABAP CDS does not support an ORDER BY clause yet. Since a "select top 5 ..." only make sense with a defined order, this feature is postponed as well.

As soon as the ORDER BY is available, the LIMITS-clause will probably be supported as well.

What is the alternative ? For the ordering you have to rely on the query language on top of CDS, say, ABAP Open SQL.

best regards

Andreas

Former Member
0 Kudos

Thanks Andreas...

SQLScript...will solve the problem...

0 Kudos

Hi Christiaan,

Great blog! Thanks for keeping it updated. I am currently creating CDS views for client handling (MANDT). You have mentioned in the blog : "Currently you can only define one CDS entity per DDL source."

Does this still hold true ? I tried to create multiple views in one DDL source but failed. Wondering if some new syntax has come in to achieve this?

former_member205436
Participant
0 Kudos

Hi Shailesh,

this still holds true for ABAP CDS and probably won't change in the near future.

Kind regards

Chris

Former Member
0 Kudos

you are true chris

Former Member
0 Kudos

Hi Christiaan,

Thanks for this fabulous survey of cds-view .

I found that there were many annotations can be used in DDL building , but i wonder if there is a list of all annotations ? Cause currently I don`t have any idea about the relationship between annotations and the view definition(define view as select......).

Regards,

Zaza

Former Member
0 Kudos
former_member205436
Participant
0 Kudos

Hi Zaza,

thanks for the update. I was just about to suggest the same ;-).

Kind regards

Chris

sunilsankar
Explorer
0 Kudos

Hi All,

I have built an CDS view with 3 tables - T1, T2, T3. T1 and T2 have 1-1 mapping while T3 has multiple entries. I tried all options to get last(one) row from T3 but couldn't able to achieve it. Getting multiple rows from the query.

Is there any possible way to exclude duplicates from table 3?

Appreciate if you help me on this.

former_member185490
Participant
0 Kudos

Hi Christiaan,

Is this the future direction to build all views in ABAP itself and not use Hana Calculation views? Can you pl. provide some more insigts if SAP continue to suggest do calc views and cds views?

Former Member
0 Kudos

hi Andreas,

Will the nested select be supported in future? besides, does abap cds support while loop?

thanks,

Anni

0 Kudos

Hi Anni,

subselects are certainly on the backlog for the CDS Scrum team, unfortunately we have a long backlog.

Regarding Loops, since CDS like SQL is a purely declarative language I do not think we will have any procedural elements like loops in the near future.

If you need a loop, use SQL Script.

regards

Andreas

hardyp180
Active Contributor
0 Kudos

Hello!

I just need to be sure about this - you just said in no uncertain terms that loops are possible in SQL Script.

Can you just confirm that once again? I had it in my head that this was not the case, maybe it was not once, but maybe now this has been added.

So just to be clear - when you say "if you need a loop, use SQL Script" that means SQL script supports loops?

An example of the syntax would be wonderful, I bet it is not like ABAP, probably more like a so called "cursor" moving through a group of database records.

Cheersy Cheers

Paul

0 Kudos

Hi Paul,

yes I can confirm loops in SQLScript, just look at the SQLScript reference here in the SCN. In the subsection Imperative SQLScript Logic-> Orchestration-Logic it says: Orchestration logic is used to implement data flow and control flow logic using imperative language constructs such as loops and conditionals.

regards

Andreas

Former Member
0 Kudos

Hi all ,

Anybody know how to hide this navback button thought annotations ?

I cannot find any annotation working on it .

Thanks&BestRegards

BryanPierce
Explorer
0 Kudos

Hi,

The link to Carine's video (http://scn.sap.com/docs/DOC-47994) is returning "Access to this place or content is restricted...". Any suggestions?

CarineTchoutouo
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Bryan,

please try again. The document should now be accessible.

Kind regards

BryanPierce
Explorer
0 Kudos

Link works great now. Thanks!

Astashonok
Participant
0 Kudos
As I see it, in latest Eclipse Oxygen there is no such entity as DDL Source, while creating, and one should choose Data Definition with proper template (View ot Table Function). Yeah?
Balu483
Participant
0 Kudos
Currently you can only define one CDS entity per DDL source. What it means?

In future SPs, SAP is planing to define multiple CDS entities per one DDL source?