AMDP Part 3: Procedures, Functions and Table Function Implementations
Know the three kinds of AMDP methods — procedures, scalar/table functions and CDS table function implementations — their syntax, how they are called, and when to use each.
Explanation
📌 KEY POINTS
✅ AMDP procedure: called from ABAP like a method; can have several IMPORTING/EXPORTING parameters.
✅ AMDP function (BY DATABASE FUNCTION): returns one result; used from other AMDPs, not directly from ABAP.
✅ FOR TABLE FUNCTION: implements a CDS table function; consumed via the CDS entity.
💡 Reuse SQLScript logic by calling AMDP functions/procedures inside other AMDPs (list them in USING).
━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE
Part 2 built a procedure. AMDP has three flavours; picking the right one keeps code reusable.
━━━━━━━━━━━━━━━━━━━━
▶ 1. AMDP PROCEDURE
- Syntax: METHOD m BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT [OPTIONS READ-ONLY] USING …
- Signature: IMPORTING and EXPORTING (and CHANGING) parameters, all passed BY VALUE; no RETURNING.
- Called from ABAP like any static or instance method: zcl_x=>m( EXPORTING … IMPORTING … ).
- Can read and (without READ-ONLY) write database tables — writing is rare and needs strong justification.
- Can call other AMDP procedures.
━━━━━━━━━━━━━━━━━━━━
▶ 2. AMDP FUNCTION (SCALAR OR TABLE)
- Syntax: METHOD f BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY USING …
- Signature: IMPORTING parameters and a RETURNING parameter (a table type or, for scalar functions, an elementary type).
- Not called from ABAP directly: it is used in SQLScript of other AMDPs, e.g. SELECT * FROM "ZCL_X=>F"( :param ); — and the function is listed in the caller's USING as zcl_x=>f.
- Good for reusable building blocks (e.g. "open requests per supplier").
━━━━━━━━━━━━━━━━━━━━
▶ 3. CDS TABLE FUNCTION IMPLEMENTATION
- CLASS-METHODS m FOR TABLE FUNCTION ztf_… — signature comes from the CDS definition.
- Implemented BY DATABASE FUNCTION; returns the CDS "returns" structure.
- Consumed through the CDS entity by any consumer (ABAP SQL, CDS, OData).
- Covered in the CDS Table Functions series.
━━━━━━━━━━━━━━━━━━━━
▶ 4. WHICH ONE?
- A result needed by an ABAP program only → procedure (or better: ABAP SQL/CDS).
- A result needed by CDS, services or analytics → CDS table function.
- A reusable SQLScript step for other AMDPs → AMDP function.
━━━━━━━━━━━━━━━━━━━━
▶ 5. OPTIONS
- READ-ONLY — required for functions; recommended for read procedures.
- CDS SESSION CLIENT CURRENT / DEPENDENT (depending on release) — lets AMDP work with the CDS client session variable for client-safe reads of CDS views inside SQLScript (Part 4).
━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP
- Create an AMDP function get_open_by_supplier returning a table.
- Call it inside the procedure from Part 2 (code example).
- Check USING lists the function.
- Activate and call the procedure from ABAP.
✅ CHECKPOINT: you can choose and combine AMDP method types.
Code example
CLASS zcl_sales_amdp DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. INTERFACES if_amdp_marker_hdb. TYPES: BEGIN OF ty_open, customer TYPE c LENGTH 10, open_items TYPE i, END OF ty_open, tt_open TYPE STANDARD TABLE OF ty_open WITH EMPTY KEY. " 1. procedure: called from ABAP CLASS-METHODS get_customer_report IMPORTING VALUE(iv_company_code) TYPE char4 EXPORTING VALUE(et_open) TYPE tt_open. " 2. table function: reusable building block for other AMDPs CLASS-METHODS open_items_by_customer IMPORTING VALUE(iv_company_code) TYPE char4 RETURNING VALUE(rt_open) TYPE tt_open. " 3. CDS table function implementation (signature from CDS) " CLASS-METHODS get_aging_rank FOR TABLE FUNCTION ztf_purchreqagingrank.ENDCLASS. CLASS zcl_sales_amdp IMPLEMENTATION. METHOD open_items_by_customer BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY USING zsales_item. RETURN SELECT customer, COUNT(*) AS open_items FROM zsales_item WHERE mandt = SESSION_CONTEXT( 'CLIENT' ) AND company_code = :iv_company_code AND status = 'OPEN' GROUP BY customer; ENDMETHOD. METHOD get_customer_report BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY USING zcl_sales_amdp=>open_items_by_customer. " function in USING et_open = SELECT customer, open_items FROM "ZCL_SALES_AMDP=>OPEN_ITEMS_BY_CUSTOMER"( :iv_company_code ) WHERE open_items > 5 ORDER BY open_items DESC; ENDMETHOD.ENDCLASS. " ===== Calling the procedure from ABAP =====" zcl_sales_amdp=>get_customer_report(" EXPORTING iv_company_code = '1010'" IMPORTING et_open = DATA(customers) )." zcl_sales_amdp=>open_items_by_customer( ... ) -> NOT callable from ABAPReal project scenario
Three AMDP procedures in a project each contained the same 30-line SQLScript block that determined "open items per customer", with small differences that produced inconsistent numbers across reports.
The block was extracted into one AMDP table function and called from the three procedures (listed in their USING clauses). Numbers became consistent, and a later change to the definition of "open" was made in one place only.
Common mistakes
- Trying to call an AMDP function directly from ABAP.
- Forgetting to list called AMDPs in USING.
- Using RETURNING in procedures or EXPORTING in functions.
- Parameters not passed by VALUE.
- Copying SQLScript blocks instead of creating reusable functions.
- Using a procedure where a CDS table function would serve more consumers.
Best practices
- Procedures for ABAP callers, functions for reuse, table functions for CDS consumers.
- READ-ONLY by default.
- List every used table, view and AMDP in USING.
- Extract shared SQLScript into AMDP functions.
- Prefer CDS table functions when results feed services or analytics.
Interview angle
Questions: "Difference between AMDP procedure and AMDP function?", "How do you reuse SQLScript logic?", "What is FOR TABLE FUNCTION?"
Strong answer: procedures (BY DATABASE PROCEDURE) are called from ABAP with IMPORTING/EXPORTING by-value parameters; functions (BY DATABASE FUNCTION, READ-ONLY, RETURNING) are used inside other AMDPs and must appear in their USING; FOR TABLE FUNCTION methods implement CDS table functions and are consumed via the CDS entity. Reuse logic through AMDP functions.
Practice this lesson
Quick check
1. When an AMDP method implements a CDS table function, what must match precisely between the CDS table function signature and the AMDP method signature?
2. What is the primary purpose of implementing a CDS table function using an AMDP method?
3. How is a CDS table function typically connected to its AMDP implementation in the CDS source code?
Interview questions
1. Three AMDP procedures contain the same SQLScript block with slight differences, and reports show inconsistent numbers. What do you do?
Answer it in your own words first, then compare.
2. Explain the difference between AMDP procedures, AMDP functions and FOR TABLE FUNCTION methods.
Answer it in your own words first, then compare.
Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.