ABAP CDS Table Function with AMDP: Code Generator
This generator writes a complete ABAP CDS table function: the DDL definition, the AMDP class that implements it in SQLScript and the ABAP SQL statement that reads it. The default sums VBAK net value by sales organisation, month and currency on S/4HANA or any ABAP 7.50+ system on HANA.
Fill in
Part 1: CDS table function (DDL source in ADT)
@EndUserText.label: 'Sales order value by month'
@ClientHandling.type: #CLIENT_DEPENDENT
@ClientHandling.algorithm: #SESSION_VARIABLE
define table function ZTF_SALES_BY_MONTH
with parameters
p_from_date : abap.dats
returns
{
key mandt : abap.clnt;
key vkorg : vkorg;
key period : abap.char(6);
key waerk : waerk;
net_value : abap.dec(17,2);
}
implemented by method ZCL_TF_SALES_BY_MONTH=>get_data;Part 2: AMDP class
CLASS ZCL_TF_SALES_BY_MONTH DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
CLASS-METHODS get_data FOR TABLE FUNCTION ZTF_SALES_BY_MONTH.
ENDCLASS.
CLASS ZCL_TF_SALES_BY_MONTH IMPLEMENTATION.
METHOD get_data BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING vbak.
RETURN SELECT mandt,
vkorg,
LEFT(erdat, 6) AS period,
waerk,
CAST(SUM(netwr) AS DECIMAL(17,2)) AS net_value
FROM vbak
WHERE mandt = SESSION_CONTEXT('CLIENT')
AND erdat >= :p_from_date
GROUP BY mandt, vkorg, LEFT(erdat, 6), waerk;
ENDMETHOD.
ENDCLASS.Part 3: Consume it in ABAP
DATA lv_from TYPE d VALUE '20260101'. SELECT vkorg, period, waerk, net_value FROM ZTF_SALES_BY_MONTH( p_from_date = @lv_from ) ORDER BY vkorg, period INTO TABLE @DATA(lt_sales). cl_demo_output=>display( lt_sales ).
Needs ABAP 7.50 or later on SAP HANA. Create Part 1 as a Data Definition and Part 2 as an ABAP class in ABAP Development Tools (Eclipse); SE80 cannot edit AMDP or DDL sources. Activate the class and the table function together. The RETURN column names and order must match the returns list exactly. For a client-independent function use @ClientHandling.type: #CLIENT_INDEPENDENT, drop the mandt key and the SESSION_CONTEXT filter. Check annotation values in the ABAP keyword documentation for your release.
Get your table function reviewed
Send us the DDL and SQLScript and we will check client handling, types and whether a CDS view entity would do the job.
Optional. The tool and every export work without this. No spam.
How to use it
Enter the table function name, label, class and method names and the parameter name. In ABAP Development Tools create a Data Definition with the Define Table Function template and paste Part 1, then create the class and paste Part 2, and activate both together because each refers to the other. Part 3 is a consumer you can drop into a report or a test class. To change the logic, edit only the SQLScript inside the method and the returns list, keeping both in the same order with the same names.
- First returned field must be the client for client-dependent functions.
- List every table read in the USING clause.
- OPTIONS READ-ONLY is required for table function methods.
How the pieces connect
The CDS table function is only a signature: parameters, a typed result and the name of the method that implements it. The method is an AMDP declared with FOR TABLE FUNCTION and implemented BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT, so it is created as a HANA table function and executed in the database. The class must implement the marker interface IF_AMDP_MARKER_HDB. With @ClientHandling.algorithm #SESSION_VARIABLE, ABAP SQL does not filter the client for you inside SQLScript, so the WHERE mandt = SESSION_CONTEXT('CLIENT') line matters; without it the function returns data from every client.
CDS view vs CDS table function
A CDS view entity is declarative: joins, associations, CASE, aggregations and built-in functions, with authorisation checks through DCL and full support in RAP, OData and analytical queries. Use it whenever the logic fits, because the database optimiser and SAP tooling understand it. A table function is for logic CDS cannot express: window functions such as ROW_NUMBER over partitions, procedural SQLScript, complex string handling, or HANA-specific functions. The cost is that it is HANA-only, opaque to the CDS optimiser, and harder to test and to extend. Many teams wrap a table function in a CDS view entity to add associations and access control.
Gotchas
ERDAT is stored as an 8-character string, so LEFT(erdat, 6) gives YYYYMM; on real data you may prefer the billing or posting date. SUM over NETWR across currencies is meaningless, which is why the currency is part of the key here. Type mismatches between the SQLScript result and the returns list fail at runtime, so cast aggregates explicitly. Parameters are read with a colon prefix in SQLScript. Table functions cannot be edited in SE11 or SE80, and transports carry both the DDL source and the class, which must travel together.
Frequently Asked Questions
What is an ABAP CDS table function?
It is a CDS entity whose result is computed by an AMDP method written in SQLScript instead of a SELECT definition. The DDL source declares parameters and returned fields and names the implementing class method. ABAP SQL reads it like a view, passing parameters in brackets after the name.
When should I use a CDS table function instead of a CDS view?
Only when the logic cannot be expressed in a CDS view entity, for example window functions, procedural steps or HANA-specific SQLScript functions. Views are portable across databases, optimised better and supported by RAP, DCL and analytics. A table function ties the code to HANA and adds a class to maintain.
How do I handle the client in a CDS table function?
For client-dependent data use @ClientHandling.type #CLIENT_DEPENDENT and @ClientHandling.algorithm #SESSION_VARIABLE, make the client the first returned key field, and filter WHERE mandt = SESSION_CONTEXT('CLIENT') in the SQLScript. Without that filter, rows from all clients are returned.
Can I create a CDS table function in SE11 or SE80?
No. CDS DDL sources and AMDP method bodies are edited in ABAP Development Tools for Eclipse. SE80 can display the class but not edit the SQLScript. Create the Data Definition and the class in ADT, activate them together and assign both to the same transport request.
If you want sales and finance users to ask S/4HANA questions like this in plain English, with the query visible and running on your own servers, we can set that up.
Related Tools
ABAP CDS View Skeleton Generator
Fill in table, fields, aliases and one association to get CDS view DDL in both the classic DDIC-based and the view entity syntax.
Developer ToolsABAP Selection Screen and SELECT-OPTIONS Generator
Builds a complete ABAP report with a framed selection screen, two SELECT-OPTIONS, a parameter, radio buttons that hide a field, event blocks, validation, an optional option restriction and an ALV output.
SAPABAP SELECT Statement Generator
Fill in table, fields and a WHERE condition and get modern ABAP Open SQL: SELECT INTO TABLE, SELECT SINGLE and a safe FOR ALL ENTRIES pattern.
SAPSAP HANA Memory Usage SQL Generator (Used, Resident, Heap)
Builds five monitoring queries for a HANA tenant: used memory against the allocation limit per service, host resident memory, top heap allocators, top column tables and daily used-memory history.
More Developer Tools tools
See all- ABAP CDS Annotations Reference (Common On-Premise Annotations)
- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton
- ABAP Custom Exception Class Generator
Keep exploring
Free tools
- ABAP CDS View Skeleton GeneratorFill in table, fields, aliases and one association to get CDS view DDL in both the classic DDIC-based and the view entity syntax.
- ABAP CDS Annotations Reference (Common On-Premise Annotations)The annotations you actually write in on-premise CDS views, with allowed values, scope, effect and the difference between DDIC-based views and view entities.
- SAP BW Transformation Routine Generator (Start, End, AMDP)Generate the body of a BW transformation start routine, end routine with an ADSO lookup, field routine, or the equivalent AMDP SQLScript routines for HANA push-down.
ERP fixes and how-tos
- Private AI layer for SAP S/4HANA on-premise: released APIs, CDS and a local modelAdd a private AI assistant to SAP S/4HANA on-premise or private edition: released APIs, CDS views, ODP, retrieval index, local LLM and access control.
- API Creation Process in SAP S/4HANA: From CDS View to OData ServiceHow to create a custom API in SAP S/4HANA: CDS view entity, service definition, service binding, activation, authorization and when to use the Business Hub.
- SAP S/4HANA Standard Reports: Fiori, Embedded Analytics and CDS ViewsWhere standard reports live in SAP S/4HANA: Fiori analytical apps, embedded analytics and CDS views, with examples for sales reporting and Group Reporting in finance.