Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert
83,662

Recap

With ABAP 7.40, SP05 the ABAP Core Data Services, short ABAP CDS,  were introduced. The ABAP Core Data Services implement the general CDS concept of SAP (that is also available for HANA as HANA CDS) for AS ABAP. They use a DDL to define CDS views that implement a semantic data model in the ABAP Dictionary.

The power of ABAP CDS is, that it is open, meaning database independent (with some exceptions if the databases do not yet cover all the functionalities as e.g. views with parameters). You can use the DDL of ABAP CDS in Eclipse based ADT to create rather complex views that exceed the capabilities of the classical database views created in SE11 by far. Technically, from the source code of a CDS view, a classical database view is generated. We call this the CDS database view. You can display the fields of this view in SE11 but not change them. Especially, you can see the real database object - the SQL view - that is created from the view definition on the database in SE11. But this CDS database view serves mainly the internal technical purpose to realize the view in the dictionary. You can, but you should not use it in ABAP programs. Instead, you work with the CDS entitiy, whose name is defined behind DEFINE VIEW. Only the entity carries the full capabilities of the CDS view, like semantical information, client handling, connection to authority checks (planned), and so on. You can use the CDS entity behind TYPE for declaring work areas and in Open SQL in ABAP programs.

You use these views for the famous code push down in order to put more analytic work to the database. If the functionality of a view is only needed once, of course you can still use Joins, SQL expressions, subqueries, ... in Open SQL for this code push down. But if you want to reuse a view, need semantical or technical capabilities of CDS that exceed those of Open SQL (but we try to keep the technical capabilities on the same level, e.g., CDS knows UNION, Open SQL will know UNION with an upcoming release) or you just want to push down the full data model to the database, you use CDS. And in fact I've seen very, very extensive sophisticated views already now (oh boy ...).

But I'm a simple mind, not a business modelling guru. In order to understand the basics, I work with simple examples. Just to give you two of those:

@AbapCatalog.sqlViewName: 'DEMO_CDS_JOIN'

define view demo_cds_scarr_spfli

  (id, carrier, flight, departure, destination)

  as select from spfli

            join scarr on scarr.carrid = spfli.carrid

     { key spfli.carrid,

       key scarr.carrname,

       key spfli.connid,

       spfli.cityfrom, 

       spfli.cityto }

This is a simple join as you could also define it in classical SE11. The name of the CDS entity is demo_cds_scarr_spfli and the name of the generated CDS database view is DEMO_CDS_JOIN. One difference to a classical view is the fact, that a CDS entity that uses client dependent data sources is client dependent by default but never has a client field. The client handling is done implicitly without shining through here.

Another view, that does exactly the same as the above one but shows a new kind of modelling capability:


@AbapCatalog.sqlViewName: 'DEMO_CDS_ASSOC'

define view demo_cds_association

  (id, carrier, flight, departure, destination )

  as select from spfli

            association [1..1] to scarr as spfli_scarr

              on $projection.carrid = spfli_scarr.carrid

     { key spfli.carrid,

       key spfli_scarr.carrname,

       key spfli.connid,

       spfli.cityfrom, 

       spfli.cityto }

The join is replaced by an association, which is a more elegant way to code reusable joins. Associations can be adressed inside the view using path expressions (spfli_scarr.carrname is the most simple one) and can be published (not shown here) for usage in other views and in Open SQL (planned for SP12).

In an ABAP program you can read the views with Open SQL:

SELECT *
       FROM demo_cds_association
       ORDER BY id, carrier, flight
       INTO TABLE @DATA(result1).

SELECT *
       FROM demo_cds_scarr_spfli
       ORDER BY id, carrier, flight
       INTO TABLE @DATA(result2).

ASSERT result1 = result2.

To find out more, see the CDS documentation (it contains an authorization concept already, but the release of that was postponed to SP12).

After this recap, now the most important

News for CDS with 7.40, SP08

Besides many smaller enhancements, the most important news are:

Many new built-in functions

Besides the function already introduced with SP05, you can use standard functions as COALESCE, CONCAT, REPLACE, ABS, DIV, DIVISION, FLOOR, MOD and ROUND now and as in Open SQL the searched CASE was introduced.

Special functions  CURRENCY_CONVERSION, UNIT_CONVERSION und DECIMAL_SHIFT allow you to deal with data of the (in)famous SAP specific dictionary types CURR, CUKY, QUAN and UNIT.

@AbapCatalog.sqlViewName: 'DEMO_CDS_CRRCNV'

  define view demo_cds_currency_conversion

   with parameters to_currency:abap.cuky(5),

                   exc_date:abap.dats

   as select from demo_prices

   { id,

     currency_conversion( amount => amount,

                          source_currency => currency,

                          round => 'X',

                          target_currency => :to_currency,

                          exchange_rate_date => :exc_date,

                          error_handling => 'SET_TO_NULL' ) as amount,

     :to_currency as currency }

Parameter views

On databases that support it (not all up to now, but SAP HANA does) you can add importing parameters to a view that can be used in the SELECT statement of the view and that can be supplied when using the view in Open SQL.

Example for such a view:

@AbapCatalog.sqlViewName: 'DEMO_CDS_PARA'

define view demo_cds_parameters

  with parameters p_distance_l:S_DISTANCE,

                  p_distance_o:S_DISTANCE,

                  p_unit:S_DISTID

  as select from spfli         

            { key carrid,

              key connid,

                  cityfrom,

                  cityto,

                  distance,

                  distid }

            where distid = :p_unit and

                           distance between :p_distance_l

                                        and :p_distance_o;

Usage in Open SQL:

IF cl_abap_dbfeatures=>use_features(

     EXPORTING

        requested_features =

          VALUE #( ( cl_abap_dbfeatures=>views_with_parameters ) ) ).

  SELECT *

         FROM demo_cds_parameters( p_distance_l = @from_distance,

                                   p_distance_o = @TO_distance,

                                   p_unit       = @unit )

         ORDER BY carrid, connid

         INTO TABLE @DATA(result).

      cl_demo_output=>display( result ).

ELSE.

  cl_demo_output=>display(

    'Database system does not support views with parameters' ).

ENDIF.

Extending views

Classical views delivered by SAP or partners can be extended with the well known clasical append views. In order to achieve the same for CDS views a new DDL statement is introduced:

@AbapCatalog.sqlViewAppendName: 'DEMO_CDS_EXTENS'

extend view demo_cds_original_view with demo_cds_view_extension

  { spfli.distance,

    spfli.distid as unit };

It adds new fields to the existing view fields. Technically, a classical append view - its name defined behind the annotation @AbapCatalog.sqlViewAppendName - is generated in the ABAP Dictionary for that purpose.

98 Comments
Labels in this area