Developer ToolsFree generatorPDF, Excel, Word exportSAP S/4HANA (on-premise)All SAP S/4HANA (on-premise) tools

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

Generated code
Download:

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.

Was this tool useful?

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.

ArchRay builder
Design the on-prem AI stack for SAP HANA and Basis: model, GPUs, RAG pipeline, guardrails
See memory, speed and cost as you swap parts, then export the diagram. Free, in your browser.
Open the builder

Keep exploring

Free tools

ERP fixes and how-tos

AI on your ERP

Integrations