Skip to main content

Module 21: AMDP (ABAP Managed Database Procedures)

AMDP (ABAP Managed Database Procedures) allow ABAP developers to write database-side logic in SQLScript, managed and lifecycle-controlled by ABAP.
AMDP is the last level of code pushdown when Open SQL or CDS is not sufficient.


1. What is AMDP?

Definition

AMDP allows ABAP classes to contain database procedures or functions that are:

  • Executed in the database (SAP HANA)
  • Written in SQLScript
  • Managed, activated, and transported via ABAP
Key Concept

AMDP code runs entirely on the database, not on the ABAP application server.


2. Why AMDP Exists

Limitations of Open SQL & CDS

  • Some calculations are too complex
  • Procedural logic required
  • Iterative processing at DB level
  • Performance-critical transformations

AMDP fills this gap.

SAP Recommendation

Use AMDP only when CDS or Open SQL cannot solve the problem cleanly.


3. AMDP Basics


3.1 AMDP Class Definition

An AMDP must be:

  • A global ABAP class
  • Implement interface IF_AMDP_MARKER_HDB
CLASS zcl_sales_amdp DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.

PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.

CLASS-METHODS get_sales_data
IMPORTING
iv_year TYPE i
EXPORTING
et_data TYPE STANDARD TABLE.
ENDCLASS.

3.2 AMDP Method Implementation

CLASS zcl_sales_amdp IMPLEMENTATION.

METHOD get_sales_data
BY DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY.

et_data =
SELECT company,
SUM(amount) AS total_amount
FROM zsales
WHERE year = :iv_year
GROUP BY company;

ENDMETHOD.

ENDCLASS.
Syntax Note
  • : is used to reference ABAP parameters

  • SQLScript syntax applies inside the method

4. SQLScript Introduction

What is SQLScript?

SQLScript is:

  • SAP HANA’s procedural SQL language

  • Supports variables, loops, conditions

  • Optimized for in-memory processing

Example SQLScript Logic

DECLARE lv_total DECIMAL(15,2);

lv_total :=
SELECT SUM(amount)
FROM zsales
WHERE year = :iv_year;
SQLScript ≠ Open SQL

SQLScript is HANA-specific and not portable.

5. CDS Table Functions with AMDP

CDS Table Functions combine:

  • CDS modeling

  • AMDP execution

5.1 CDS Table Function Definition

define table function Z_TF_Sales
returns {
company : bukrs;
total : abap.curr(15,2);
}
implemented by method
zcl_sales_amdp=>get_sales_tf;

5.2 AMDP Method for Table Function

METHOD get_sales_tf
BY DATABASE FUNCTION
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY.

RETURN
SELECT company,
SUM(amount) AS total
FROM zsales
GROUP BY company;

ENDMETHOD.
Real-World Usage

CDS Table Functions are heavily used in:

  • Complex analytics

  • BW-like transformations

  • RAP data sources

6. Performance Use Cases for AMDP

When AMDP Makes Sense

  • Very large datasets

  • Complex transformations

  • Multiple aggregations

  • Procedural DB logic

  • BW-style processing

When AMDP Does NOT Make Sense

Avoid AMDP When
  • Logic is simple

  • CDS can handle it

  • Readability suffers

  • Business logic is involved

  • Portability is required

7. Error Handling in AMDP

AMDP does not support ABAP TRY…CATCH inside SQLScript.

7.1 SQLScript Error Handling

  • Use conditional logic

  • Validate inputs

  • Raise errors explicitly

IF :iv_year < 2000 THEN
SIGNAL SQL_ERROR 'Invalid year';
END IF;

7.2 Handling Errors in ABAP

TRY.
zcl_sales_amdp=>get_sales_data(
iv_year = lv_year
IMPORTING et_data = lt_data
).
CATCH cx_amdp_execution_failed INTO DATA(lo_exc).
MESSAGE lo_exc->get_text( ) TYPE 'E'.
ENDTRY.
Best Practice

Always handle CX_AMDP_EXECUTION_FAILED in ABAP.

8. AMDP vs CDS (Quick Decision Guide)

ScenarioRecommendation
Simple joins & filtersCDS
AnalyticsCDS
Complex DB logicAMDP
Procedural processingAMDP
RAP data modelCDS first

9. Common Mistakes

Avoid These
  • Writing business logic in AMDP

  • Using AMDP for simple SELECTs

  • Ignoring error handling

  • Overusing SQLScript loops

  • Forgetting portability impact

10. Summary

  • AMDP executes logic in the database

  • Written in SQLScript

  • Managed via ABAP classes

  • Used for complex, performance-critical logic

  • CDS should be preferred where possible

  • Error handling must be explicit

11. Practice Exercises

  • Create a simple AMDP procedure.

  • Implement a CDS table function using AMDP.

  • Compare CDS vs AMDP for a use case.

  • Handle an AMDP execution error in ABAP.

  • Refactor heavy ABAP logic into AMDP.

12. What’s Next?

➡️ Module 22: Performance Optimization (HANA)

Learning Tip

AMDP is powerful—but powerful tools must be used sparingly and wisely.