calculation example (ABAP, Open SQL, CDS)

Dear community, Jelena Perfiljeva recently reminded me by one of her comments how useful examples are. At least I feel that I can learn well from examples, too. In the beginning, very simple and basic examples are ideal to understand the possibilities or the underlying idea.

Since I am currently exploring ABAP Core Data Services (ABAP CDS) and the possibilities of Open SQL, I built a small example.

It’s about the addition of value pairs that come from a database table. The example is intended to show the different ways in which this addition can be done. I didn’t worry about the performance and I don’t think one of the solutions is better or worse. For me, that would depend on the overall context. However, due to the simplicity of the example, this context doesn’t exist here.

What do you think? Please comment.

This is my database table ZCALC in transaction SE12.

If you look the same database table via ABAP Development Tools for Eclipse (ADT), you will get this view. Please note that I created the database table via ADT.

@EndUserText.label : 'calculation example'
@AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zcalc {
  key client  : abap.clnt not null;
  key example : numc2 not null;
  summand_1   : int4;
  summand_2   : int4;

I’ve made some example entries.

Here’s my ABAP CDS design per Data Definition Language (DDL).

@AbapCatalog.sqlViewName: 'ZCDSCALCEXAMPLE'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'calculation example'
define view ZCDSCALC
  as select from zcalc
  key example,
      summand_1 + summand_2 as addition_result

Finally, the source code. I show the same idea in three ways: via ABAP, Open SQL and CDS.

REPORT zcalculation_example.

  " ABAP based
  TYPES: BEGIN OF calculation,
           example         TYPE numc2,
           summand_1       TYPE int4,
           summand_2       TYPE int4,
           addition_result TYPE int4,
         END OF calculation.

  TYPES calculations TYPE TABLE OF calculation WITH KEY example.

  DATA abap_based_result TYPE calculations.

  SELECT * FROM zcalc
           INTO CORRESPONDING FIELDS OF TABLE abap_based_result.

  LOOP AT abap_based_result ASSIGNING FIELD-SYMBOL(<row>).
    <row>-addition_result = <row>-summand_1 + <row>-summand_2.

  " Open SQL based
  SELECT example,
         summand_1 + summand_2 AS addition_result
         FROM zcalc
         INTO TABLE @DATA(open_sql_based_result).

  " CDS based
  SELECT * FROM zcdscalc
           INTO TABLE @DATA(cds_based_result).

  IF abap_based_result = open_sql_based_result AND 
     open_sql_based_result = cds_based_result.
    cl_demo_output=>display( abap_based_result ).

All three ways (ABAP, Open SQL and CDS) deliver the same result.

I would like to have Calculon check the result. Unfortunately, he is no longer available 😉 However, the calculations seem to be correct.

Here are some ressources I used:

Special thought provoking note for Ged Hurst: “What if you were developing with ABAP for the first time and someone told you that there are three different solutions for the same requirement in three different technologies?🙂


Best regards, thanks for reading and have a great time



P.S.: Please support the virtual wishing well.

P.S.S.: Not tired of reading blogs? Check this blog by Enno Wulff. I really recommend it.

Original Article:

Related blogs


Please enter your comment!
Please enter your name here