Code examples · Modern ABAP · Advanced
AMDP and a CDS table function: aggregate orders in SQLScript on HANA
The problem
A dashboard needs the number of orders and the total amount per customer from a table with millions of rows.
How we solve it
- 1DeclareCDS table function
- 2ImplementAMDP, SQLScript
- 3UseABAP SQL
The result
One row per customer with the number of orders and the sum of the amounts, calculated on the database.
1. The requirement
A dashboard needs the number of orders and the total amount per customer from a table with millions of rows. The logic is joined, grouped and summed in SQLScript on the HANA database and must be callable like any other CDS view from ABAP and from a Fiori app.
2. How we design it
A CDS table function declares the shape of the result: its fields and the method that fills it. The method lives in an AMDP class, an ordinary ABAP class that implements the marker interface IF_AMDP_MARKER_HDB. The method body is SQLScript, which the database runs next to the data. From outside, the table function is selected like any other view. Only the data that the dashboard needs travels to the application server.
Why we decided it this way
- 1A table function, not a loop in ABAPSumming millions of rows in ABAP means moving them all to the application server first. The database does it where the data is.
- 2SQLScript only where plain CDS cannot do itSimple grouping and sums are possible in a normal CDS view with aggregation, which is easier to maintain and runs on every database. An AMDP is for logic that CDS cannot express.
- 3The client as a key field of the resultThe tables are client-dependent. AMDP does not filter the client for you, so the function takes the current client as a parameter, filters on it in the SQLScript and returns it as the first key field.
- 4READ-ONLY and USINGREAD-ONLY promises that the procedure does not change data and USING names the tables the procedure reads, so the dependency is known.
Before you start
- The table ZORDER from the CDS join example with its demo data (or any client-dependent table with the same fields).
- A Standard ABAP system on SAP HANA (S/4HANA on-premise or private cloud). AMDP does not run on other databases.
3. The code, step by step
Step 1 of 3: Declare the table function
The result shape and the method that fills it.
// RETURNS and IMPLEMENTED BY
@EndUserText.label: 'Order totals per customer'
@ClientHandling.type: #CLIENT_DEPENDENT
define table function ZTF_ORDER_TOTALS
with parameters
@Environment.systemField: #CLIENT
clnt : abap.clnt
returns {
key client : abap.clnt;
key customer_id : abap.numc(6);
order_count : abap.int4;
@Semantics.amount.currencyCode : 'currency'
total_amount : abap.curr(15,2);
currency : abap.cuky;
}
implemented by method zcl_amdp_order_totals=>get_totals;
Part by part (plain text)
- RETURNS and IMPLEMENTED BY. WITH PARAMETERS receives the current client, which the system fills automatically through the annotation Environment.systemField. The annotation ClientHandling.type marks the function as client-dependent. RETURNS lists the fields of the result. The first field is the client and the key identifies one row per customer. IMPLEMENTED BY METHOD names the AMDP method that delivers the rows. The amount field points to the currency field through the Semantics annotation, as in any amount.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
One row per customer with the number of orders and the sum of the amounts, calculated on the database.
What you should see
Example output with the demo data of the CDS join example: Customer 000001: 1 order(s), 100.00 EUR Customer 000009: 1 order(s), 50.00 EUR
Try it and check the result
- Activate the table function and the AMDP class together, then the runner. Run the runner with F9.
- Open the table function in ADT and press F8 (data preview): you see the same rows.
- Measure it: run the SQL trace (transaction ST05) around the runner, or the ABAP runtime analysis (SAT), and compare with a version that reads all rows into ABAP and sums them in a loop. The difference grows with the table.
Common mistakes
- An AMDP only runs on HANA and is harder to debug than ABAP. Use a normal CDS view with aggregation whenever it can express the logic.
- Forgetting a table in USING gives an activation or runtime error. List every table the procedure reads.
- Filter on the client yourself. AMDP has no implicit client handling, so without WHERE client = :clnt the SQLScript reads and aggregates the rows of every client before ABAP SQL filters the result.
- SQLScript errors appear at activation or first run, not in the ABAP syntax check. Test with a small data set first.
- ABAP Cloud only allows client-safe AMDP: add the ClientHandling annotations with a session variable to the table function and AMDP OPTIONS CDS SESSION CLIENT DEPENDENT to the method. The code above is written for Standard ABAP.