Вы находитесь на странице: 1из 3

ABAP CDS Table Functions Implemented in AMDP

In an ABAP CDS Table Function development, we define an entity with


the field structure, parameters (optional) and an association to a
class/method. With AMDP we’re able to write database procedures
directly in the ABAP layer and encapsulate inside the class/method we
defined in the Table Function, the call works as the same like any
other ABAP methods and we have the following advantages:
 Detailed analysis of runtime errors through ST22;
 Database procedures debug available through HANA Studio;

 Transport identical as to ABAP classes.

 Since AMDP works directly with database scripting some extra


steps are necessary like the definition of database and the
script language to be used (in SAP HANA the language is the SQL
Script). .

Scenario

 Each airline company provides flight connections to different


cities around the world, the user wants to see all the cities
supported by a specific airline in a single field separate by
comma. Since the number of cities can be different for each one
of the airlines we need to generate a logic to concatenate all
the cities no matter how many entries are returned.
 Through a common ABAP CDS View we could use a CONCAT function,
but in this case we would need to define a fixed quantity of
fields, since the CDS View can’t handle this logic dynamically
how should we proceed?
 In ABAP CDS Table Function, we can implement this with a simple
database function called STRING_AGG (String Aggregation). This
function is available in the SQL Script but currently there is no
support through ABAP CDS View.
Procedure:

1. Create the CDS entity (ZCDSTABLEFUNCTION) by choosing the


template ‘Define Table function with parameters’

@EndUserText.label: 'CDS TABLE FUNCTION'


define table function ZCDSTABLEFUNCTION
returns {
client : abap.clnt;
airline_code : s_carr_id;
airline_name : s_carrname;
cities_to : abap.string;
}
implemented by method ZAMDPFLIGHTS=>FLIGHTCONNECTIONS;
Save, Check and Activate

2. Create the AMDP Class and AMDP Method

CLASS zamdpflights DEFINITION


PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
INTERFACES IF_AMDP_MARKER_HDB.
CLASS-METHODS FLIGHTCONNECTIONS FOR TABLE FUNCTION ZCDSTABLEFUNCTION.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

CLASS zamdpflights IMPLEMENTATION.


METHOD FLIGHTCONNECTIONS BY DATABASE FUNCTION FOR HDB LANGUAGE
SQLSCRIPT OPTIONS READ-ONLY USING SFLIGHTS.

ITAB = SELECT DISTINCT SFLIGHTS.MANDT AS CLIENT,


SFLIGHTS.CARRID AS AIRLINE_CODE,
SFLIGHTS.CARRNAME AS AIRLINE_NAME,
SFLIGHTS.CITYTO AS CITY_TO
FROM SFLIGHTS;

RETURN SELECT CLIENT,AIRLINE_CODE,AIRLINE_NAME,


STRING_AGG( CITY_TO,',' ORDER BY CITY_TO) AS CITIES_TO
FROM :ITAB
GROUP BY CLIENT,AIRLINE_CODE,AIRLINE_NAME;

ENDMETHOD.
ENDCLASS.

Save, Check and Activate

3. Consuming above CDS using Open SQL in ABAP Report

REPORT ZCONSUME_CDS_AMDP.

select * from zcdstablefunction into table @data(lt_flights).


cl_demo_output=>display_data( EXPORTING value = lt_flights ).