Tuesday, May 26, 2020

SAP ABAP CDS Views: Data-to-Code vs Code-to-Data - an example with aggregation

I am going to discuss two approaches to a requirement that I have received often from customers.

EH&S: Business wants to know the specifications where there is an instance maintained for a certain property.

In EH&S it is possible to maintain more than one intance per property. Multiple property  instantes can be confusing for the business, so  customers wanted to have an evaluation if they have EH&S specifications with multiple instances (same sort) for certain properties.

Of course, it was possible to solve the requirement with the help of an ABAP Report looking something like that:

Back-End SAP, ABAP:

The logic is in my report. (Data-to-Code Approach)


REPORT zehs_r_inst_count.

TABLES: estvh, estva, estrh.

SELECT-OPTIONS: s_estcat FOR estvh-estcat DEFAULT 'SAP_EHS_1023_094'.
select-OPTIONS: s_subid for estrh-subid.

SELECT COUNT( DISTINCT estva~recn ) AS count, estva~ord,estva~recnroot, estva~recntvh, estvh~estcat, estrh~subid
INTO TABLE @DATA(lt_inst)
FROM estva INNER JOIN estvh ON estvh~recn = estva~recntvh JOIN estrh ON estva~recnroot = estrh~recnroot
WHERE estvh~estcat IN @s_estcat and estrh~subid in @s_subid AND estva~delflg EQ @space AND estvh~delflg EQ @space
and estrh~delflg eq @space
GROUP BY estva~recnroot, estva~recntvh, estva~ord, estvh~estcat, estrh~subid.

 perform show_alv.

Nonetheless, it is far more flexible and efficient to use CDS Views and not to hold so much DB logic in the application:


Eclipse, Open SQL:

The logic is contained in the view (Code-to-Data or code pushdown Approach)




For developers who are new to SAP CDS Views appear very easy, intuitive and sql-like.

I habe been usng the data-to-code a lot in the past, so that I notice a huge efficiency improvement when using CDS Views.

I can check my view in transaction se16n:


This new programming model supports clean code and reusability.
It is satisfying to clean the application from  sql statements and to push them to the data base. 

There are many options for using the newly created view:

 - If the evaluation should be pulled only once, this can be done using transaction se16n
-  If the evaluation should be done by a user, a report or transaction can be created
-  For views that will be checked often by many users, a FIORI App can be created by publishing the view as an oData Service

2 comments:

  1. Nice to get all the SQL code out off the programming code. I'll try that!

    ReplyDelete