# ABAP CDS Table Function with AMDP: Code Generator

> Source: https://netray.co/tools/abap-cds-table-function-amdp-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Builds the three parts of a CDS table function: the DDL definition with client handling and parameters, the AMDP class with SQLScript, and the ABAP SQL call that consumes it.

## Category

developer

## Erp

SAP S/4HANA (on-premise)

## Audience

- ABAP developer
- SAP technical architect

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Table function name

**Key:** tf

**Type:** text

**Default:** ZTF_SALES_BY_MONTH

#### Label

**Key:** label

**Type:** text

**Default:** Sales order value by month

#### AMDP class name

**Key:** cls

**Type:** text

**Default:** ZCL_TF_SALES_BY_MONTH

#### Method name

**Key:** meth

**Type:** text

**Default:** get_data

#### Parameter name

**Key:** par

**Type:** text

**Default:** p_from_date

#### Value used in the consumer (YYYYMMDD)

**Key:** fromdate

**Type:** text

**Default:** 20260101

**Blocks:**

#### Part 1: CDS table function (DDL source in ADT)

**Body:** @EndUserText.label: '{{label}}'
@ClientHandling.type: #CLIENT_DEPENDENT
@ClientHandling.algorithm: #SESSION_VARIABLE
define table function {{tf}}
  with parameters
    {{par}} : 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 {{cls}}=>{{meth}};

#### Part 2: AMDP class

**Body:** CLASS {{cls}} DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb.
    CLASS-METHODS {{meth}} FOR TABLE FUNCTION {{tf}}.
ENDCLASS.

CLASS {{cls}} IMPLEMENTATION.
  METHOD {{meth}} 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 >= :{{par}}
            GROUP BY mandt, vkorg, LEFT(erdat, 6), waerk;
  ENDMETHOD.
ENDCLASS.

#### Part 3: Consume it in ABAP

**Body:** DATA lv_from TYPE d VALUE '{{fromdate}}'.

SELECT vkorg, period, waerk, net_value
  FROM {{tf}}( {{par}} = @lv_from )
  ORDER BY vkorg, period
  INTO TABLE @DATA(lt_sales).

cl_demo_output=>display( lt_sales ).

**Notes:** 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.

## Content

**Intro:** 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.

**Sections:**

#### How to use it

**Body:** 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.

**Bullets:**

- 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

**Body:** 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

**Body:** 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

**Body:** 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.

**Cta:** 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.

**Faq:**

#### What is an ABAP CDS table function?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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.

## Search terms

abap cds table function, table function in cds view sap abap, sap abap cds view table function, cds view vs cds table function abap, abap cds view table function, amdp table function example

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
