Saturday, June 8, 2024

Introduction To CDS Views

 

What is a CDS View?

CDS or Core Data Services are part of SAPs Code Pushdown approach where you try to do as much as possible at database layer than application layer. One example of Code Pushdown can be calculating aggregates at database layer using aggregate functions in SQL rather than bringing all data in application layer and calculating the aggregates.

CDS offers capabilities such as relationship definitions like associations, built-in functions like currency conversion, extensions, semantic capabilities like annotations along with all SQL enhancements.

CDS was introduced in ABAP 7.40 SP05 and additional features were added subsequently in SP08, SP10, ABAP 7.50 SP00 and so on.

A sample and a simple CDS view looks like below.

@AbapCatalog.sqlViewName: 'DEMO_CDS_PRJCTN'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_spfli
  as select from
    spfli
    {
      key spfli.carrid,
      key spfli.connid,
          spfli.cityfrom,
          spfli.cityto
    }

The information we get from this is


  1. The name of the view is demo_cds_spfli
  2. At the top we have an annotation that tells us the name of related SQLView – DEMO_CDS_PRJCTN
  3. Another annotation says that Authorization check is not required
  4. It selected the data from table spfli
  5. The fields selected are carrid, connid, cityfrom and cityto with first 2 being the key fields

It is simple to read and understand, right? With this example, we can imagine a CDS view as a query written inside some kind of a reusable container which can be executed to get the data.

How to create a CDS view?

CDS views can only be created from ABAP Development Tools (ADT) in Eclipse. You can not create CDS views from SAPGUI.

To install Eclipse, check the post Install Eclipse for ABAP Development and if you are not familiar with Eclipse, visit Working with Eclipse for ABAP development .

1. Right click on your package, New > Other ABAP Repository Object

2. Search for ‘Data Definition’, select the node Data Definition and click Next

3. Enter Name and Description, then click Next

4. Select/Create Transport Request, click Next.

5. Select the template Define View and click Finish


6. The CDS view will be created and displayed like below.

7. Enter sqlViewName, edit the data_source_name and add fields. Separate the elements within { … } with a comma.

To use help entering the field name, use Ctrl + Space

8. Complete the CDS view like below.

@AbapCatalog.sqlViewName: 'ZJP_SQL_SMPL'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Simple CDS View'
define view ZJP_SIMPLE_CDS as select from spfli {
    key carrid as Carrid,
    key connid as Connid,
    countryfr as Countryfr,
    cityfrom as Cityfrom,
    airpfrom as Airpfrom,
    countryto as Countryto,
    cityto as Cityto,
    airpto as Airpto
}


9. Activate

10. To test the CDS view right click on your CDS view / Data definition and choose option

Open With > Data Preview.

11. OR, you can simply execute the CDS View with application toolbar button Run As.. and select ABAP Application.


12. Get the output as below.

In ABAP code, the CDS view can be used similar to database views or tables.

SELECT * FROM zjp_simple_cds
    INTO TABLE @DATA(cds_data_tab).
IF sy-subrc EQ 0.
  cl_demo_output=>display( cds_data_tab ).
ENDIF.

No comments:

Post a Comment