Day 8: AMDP Basics

 



Day 8: AMDP Basics

  • What is AMDP?

  • Syntax and structure

  • Use of SQLScript in methods

  • Class creation with interface IF_AMDP_MARKER_HDB

Hands-on:

  • Create a basic AMDP class with SELECT logic






To get started with AMDP, it's essential to have a basic understanding of OOPs in ABAP.

Click here to explore the contents of 

OOPs in ABAP.








🔹 1. What is AMDP (ABAP Managed Database Procedure)?

Definition:
AMDP allows you to write database procedures (typically in SQLScript) directly in ABAP classes. These are executed directly in the HANA DB layer, providing faster and optimized data processing — avoiding unnecessary data transfer between DB and application layer.

Why AMDP?

  • Push complex logic to the HANA database layer.

  • Optimize performance (Code-to-Data principle).

  • Use SQLScript (HANA’s native SQL dialect).

  • Ideal for mass data processing, aggregations, transformations.

Use cases:

  • Complex reports

  • Mass data reads

  • Aggregated KPIs

  • Real-time analytics


🔹 2. Syntax and Structure of AMDP

Basic structure involves:

  • An ABAP class implementing the interface IF_AMDP_MARKER_HDB

  • A static method with BY DATABASE PROCEDURE statement

  • The method body written in SQLScript

Syntax Template:

CLASS zcl_amdp_demo DEFINITION

  PUBLIC

  CREATE PUBLIC.


  PUBLIC SECTION.

    INTERFACES: if_amdp_marker_hdb.


    CLASS-METHODS:

      get_data

        IMPORTING value(iv_param) TYPE string

        EXPORTING value(et_result) TYPE TABLE OF your_structure

        RAISING   cx_amdp_execution_failed.

ENDCLASS.


CLASS zcl_amdp_demo IMPLEMENTATION.


  METHOD get_data BY DATABASE PROCEDURE

                  FOR HDB LANGUAGE SQLSCRIPT

                  OPTIONS READ-ONLY.


    -- Your SQLScript logic here


  ENDMETHOD.


ENDCLASS.



🔹 3. What is SQLScript? (Detailed Explanation)

SQLScript is the procedural extension of SQL provided by SAP HANA. It allows developers to write imperative logic, variables, control flow (IF, LOOP), and procedures — something standard SQL cannot handle efficiently.

Features of SQLScript:

  • Local variables (DECLARE)

  • Table variables

  • Control structures (IF, FOR, WHILE)

  • Error handling

  • Aggregations, CASE, JOINs, etc.

Example:

lt_result = 

    SELECT column1, column2

    FROM your_table

    WHERE column3 = :iv_param;

:iv_param is used to reference the ABAP input variable.


🔹 4. Use of SQLScript in AMDP Methods

You write SQLScript logic inside the AMDP method body, and you can do all your SQL-based transformations here. The method is executed inside the HANA DB engine, not ABAP runtime.


🔹 5. What is IF_AMDP_MARKER_HDB?

This is a marker interface used to declare that the class contains HANA-specific AMDP methods.

🔸 HDB = HANA Database
🔸 Required for AMDP class to execute on the HANA DB.

No methods or logic in this interface — it just flags the class for AMDP handling.


🔹 6. Class Creation with Interface IF_AMDP_MARKER_HDB

Let’s create a full working AMDP class that selects data from SFLIGHT table.


✅ Example: Basic AMDP Implementation

🔧 Step-by-step Implementation:

🔸 Step 1: Create Structure (if needed)

If required, define a structure ZSTR_FLIGHT_INFO with fields like:

  • CARRID (Type: S_CARR_ID)

  • CONNID (Type: S_CONN_ID)

  • PRICE (Type: S_PRICE)

🔸 Step 2: Create AMDP Class in Eclipse (ADT)


CLASS zcl_flight_amdp DEFINITION

  PUBLIC

  CREATE PUBLIC.


  PUBLIC SECTION.

    INTERFACES: if_amdp_marker_hdb.


    CLASS-METHODS:

      get_flight_data

        IMPORTING iv_carrid TYPE s_carr_id

        EXPORTING et_result TYPE STANDARD TABLE OF zstr_flight_info.

ENDCLASS.



CLASS zcl_flight_amdp IMPLEMENTATION.


  METHOD get_flight_data BY DATABASE PROCEDURE

                         FOR HDB LANGUAGE SQLSCRIPT

                         OPTIONS READ-ONLY

                         USING sflight.


    et_result = 

      SELECT carrid,

             connid,

             price

        FROM sflight

        WHERE carrid = :iv_carrid;


  ENDMETHOD.


ENDCLASS.


🔸 Step 3: Test the AMDP

Use an ABAP program to test:

DATA: lt_result TYPE TABLE OF zstr_flight_info.


CALL METHOD zcl_flight_amdp=>get_flight_data

  EXPORTING

    iv_carrid = 'LH'

  IMPORTING

    et_result = lt_result.


LOOP AT lt_result INTO DATA(ls_data).

  WRITE: / ls_data-carrid, ls_data-connid, ls_data-price.

ENDLOOP.



✅ Best Practices

Area

Best Practice

Naming

Prefix class with ZCL_, method names should describe logic

Performance

Use OPTIONS READ-ONLY unless you plan to update data

Reuse

Modularize SQL logic using views or reuseable procedures

Debug

Use HANA SQL Console if needed to test queries




Difference between AMDP Class and AMDP Procedure:

Topic

AMDP Class

AMDP Procedure

Definition

ABAP class that contains AMDP methods

Database procedure created automatically from AMDP

Where you work

In ABAP (SE24 or Eclipse ADT)

In HANA DB (internal runtime)

Developer writes

ABAP Class + SQLScript method

NOT manually written – generated from class

Transported via

Transport Organizer (SE09)

Controlled via ABAP transport (not separate HANA transport)

How it is executed

System calls the method → executes procedure at DB

SAP HANA internally executes procedure

Regeneration

Automatic when class is activated

Automatic recreation in HANA

Maintenance

Maintain ABAP class/method

Do NOT touch HANA directly

Example Method

SELECT, INSERT, UPDATE inside SQLScript

AMDP method is transformed into a DB procedure


🛠 How it works technically:

  1. You create an AMDP Class and AMDP Method in ABAP.

  2. When you activate, the system generates an AMDP Procedure in HANA database.

  3. When your code runs, SAP calls the HANA procedure automatically.

You never directly create or touch the procedure manually.


🔥 Simple Example

ABAP AMDP Class


CLASS zcl_sales_amdp DEFINITION

  PUBLIC

  FINAL

  CREATE PUBLIC.


  PUBLIC SECTION.

    INTERFACES: if_amdp_marker_hdb.

    CLASS-METHODS: get_sales_data

      IMPORTING value(iv_custid) TYPE kunnr

      EXPORTING value(et_sales) TYPE TABLE OF vbak.

ENDCLASS.


CLASS zcl_sales_amdp IMPLEMENTATION.


  METHOD get_sales_data BY DATABASE PROCEDURE

                         FOR HDB

                         LANGUAGE SQLSCRIPT

                         OPTIONS READ-ONLY

                         USING vbak.


    et_sales = SELECT * FROM vbak WHERE kunnr = :iv_custid;


  ENDMETHOD.


ENDCLASS.



📍Behind the scenes:

  • AMDP Class = ZCL_SALES_AMDP

  • AMDP Method = GET_SALES_DATA

  • System auto-creates a Database Procedure in HANA like /1BCAMDP/ZCL_SALES_AMDP=>GET_SALES_DATA (technical name).

You NEVER manually touch it!


✅ Summary

Feature

AMDP Class

AMDP Procedure

You Develop

✅ (Yes)

❌ (Generated)

You Maintain

✅ (Yes)

❌ (System handled)

Visible in ABAP

✅ (Class/Method)

❌ (Backend HANA object)

Update Method

Edit ABAP Class

Activate Class


🧠 Very Simple Way to Remember:

You write AMDP Class & Method → SAP builds hidden AMDP Procedure for you!




Day 8.1 AMDP Example


AMDP with JOIN, CASE, IF – Step by Step

🧠 Scenario:

We want to analyze Sales Order and Delivery Status in one shot:

  • From VBAK (Sales Order Header) and LIKP (Delivery Header)

  • We'll use JOIN, CASE, and IF logic

  • Logic: If delivery exists, mark status as 'Delivered', else 'Pending'


✅ Step-by-step Implementation:

1. Create AMDP Class


CLASS zcl_sales_amdp DEFINITION

  PUBLIC

  CREATE PUBLIC.


  PUBLIC SECTION.

    INTERFACES: if_amdp_marker_hdb.


    CLASS-METHODS: get_sales_delivery_status

      IMPORTING

        VALUE(iv_vkorg) TYPE vbak-vkorg

      EXPORTING

        VALUE(et_result) TYPE TABLE OF zsales_delivery_result.


  PROTECTED SECTION.

  PRIVATE SECTION.

ENDCLASS.


2. SQLScript Method Implementation


CLASS zcl_sales_amdp IMPLEMENTATION.


  METHOD get_sales_delivery_status BY DATABASE PROCEDURE

    FOR HDB

    LANGUAGE SQLSCRIPT

    OPTIONS READ-ONLY

    USING vbak likp.


    et_result = SELECT

        a.vbeln AS sales_order,

        a.vkorg AS sales_org,

        CASE

            WHEN b.vbeln IS NOT NULL THEN 'Delivered'

            ELSE 'Pending'

        END AS delivery_status

      FROM vbak AS a

      LEFT OUTER JOIN likp AS b

        ON a.vbeln = b.vbeln

      WHERE a.vkorg = :iv_vkorg;


  ENDMETHOD.


ENDCLASS.


🔍 Notice the CASE for delivery status and the LEFT OUTER JOIN to include all sales orders whether delivered or not.










Day 9: AMDP Advanced Concepts

  • Insert, Update, Delete in AMDP

  • Error handling and exception class

  • Performance tuning tips

Hands-on:

  • Create AMDP for aggregation and update logic



Comments

Popular posts from this blog

Day 1: Introduction to ABAP on HANA

Day 3: CDS Intermediate – Filters & Expressions

Day 2: CDS View Basics