In a previous article, we understood the term ADBC. This is the 5th Part in series on SAP HANA. This article will explain the concept of ABAP Managed Database Procedures.
From this blog, you'll learn:
1. What is AMDP in SAP HANA?
2. How to create AMDP with parameters
So let's begin.
1. What is AMDP in SAP HANA?
2. How to create AMDP with parameters
As from the prior blogs i.e CDS and HANA modeling, the thumb rule is to maximize the work in the database layer without putting a load of work on the application layer. AMDP came with the same approach but with an additional feature. But since the goal of HANA is the same, many of us get confused when to use CDS and when to use AMDP.
- ABAP Managed Database Procedures are a new feature that allows the developers to create and execute the database procedures (a.k.a function storage) in the ABAP environment using ABAP methods and ABAP data types.
- Database Procedures are used as modular programming which means create once, store the procedure and call for several times whenever required.
NOTE: AMDP is an improved ADBC as it is simply a method call.
A normal class can have both, instance methods and AMDP methods. AMDP methods are always static methods.
Here is an indication of the AMDP class and method.
- An ABAP global class that contains the interface 'IF_AMDP_MARKER_HDB' is called an AMDP class.
- A method which has the addition of 'BY DATABASE PROCEDURE' in the class implementation is the AMDP method.
Creation of ABAP Managed Database Procedure (AMDP) with Parameters
Let's take an example of the MARA table and work on AMDP Parameters.
- Open the SAP HANA studio and navigate to the ABAP perspective. Further, under your package create an ABAP class.
Key points to remember while creating an AMDP class.
- Provide interface IF_AMDP_MARKER_HDB in the class definition.
- Method parameters should be pass by value.
- ABAP class can contain AMDP methods as well as normal methods.
- AMDP methods should always be static. We write static methods as 'CLASS-METHODS'.
- Looping is not available in AMDP.
Below is the code snippet of Class Definition to fetch the details of the MARA (Material) table.
LOGIC
CLASS zcl_material_details DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES : IF_AMDP_MARKER_HDB.
TYPES : BEGIN OF TY_MATERIAL,
MATNR TYPE MATNR, "MATERIAL_NUMBER
ERSDA TYPE ERSDA, "DATE OF CREATION
ERNAM TYPE ERNAM, "NAME OF THE PERSON WHO CREATED THE OBJECT
MTART TYPE MTART, "MATERIAL TYPE
DESCRIPTION TYPE CHAR30, "MATERIAL TYPE DESCRIPTION
END OF TY_MATERIAL.
TYPES: TT_MATERIAL TYPE STANDARD TABLE OF TY_MATERIAL.
CLASS-METHODS GET_MATERIAL_DETAILS
IMPORTING
VALUE(P_MATNR) TYPE MATNR
EXPORTING
VALUE(ET_MATERIAL) TYPE TT_MATERIAL.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES : IF_AMDP_MARKER_HDB.
TYPES : BEGIN OF TY_MATERIAL,
MATNR TYPE MATNR, "MATERIAL_NUMBER
ERSDA TYPE ERSDA, "DATE OF CREATION
ERNAM TYPE ERNAM, "NAME OF THE PERSON WHO CREATED THE OBJECT
MTART TYPE MTART, "MATERIAL TYPE
DESCRIPTION TYPE CHAR30, "MATERIAL TYPE DESCRIPTION
END OF TY_MATERIAL.
TYPES: TT_MATERIAL TYPE STANDARD TABLE OF TY_MATERIAL.
CLASS-METHODS GET_MATERIAL_DETAILS
IMPORTING
VALUE(P_MATNR) TYPE MATNR
EXPORTING
VALUE(ET_MATERIAL) TYPE TT_MATERIAL.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
Below is the code snippet of Class Implementation to fetch the details of the MARA (Material) table.
CLASS zcl_material_details IMPLEMENTATION.
METHOD GET_MATERIAL_DETAILS BY DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING MARA.
ET_MATERIAL = SELECT MATNR,
ERSDA,
ERNAM,
MTART,
CASE MTART
WHEN 'ROH' THEN 'RAW MATERIAL'
WHEN 'FERT' THEN 'fINISHED PRODUCT'
WHEN 'HALB' THEN 'SEMIFINISHED PRODUCT'
END AS DESCRIPTION
FROM MARA
WHERE MATNR = P_MATNR;
ENDMETHOD.
ENDCLASS.
METHOD GET_MATERIAL_DETAILS BY DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING MARA.
ET_MATERIAL = SELECT MATNR,
ERSDA,
ERNAM,
MTART,
CASE MTART
WHEN 'ROH' THEN 'RAW MATERIAL'
WHEN 'FERT' THEN 'fINISHED PRODUCT'
WHEN 'HALB' THEN 'SEMIFINISHED PRODUCT'
END AS DESCRIPTION
FROM MARA
WHERE MATNR = P_MATNR;
ENDMETHOD.
ENDCLASS.
Upon activating the ZCL_MATERIAL_DETAILS class, you will be able to watch this class (ZCL_MATERIAL_DETAILS) in SE24.
Below is the screenshot of the SE24 transaction.
Now, let's call the AMDP method via report.
- Navigate to New ABAP Program. Provide Name and Description along with appropriate transport requests.
- Call the AMDP method and display the data.
LOGIC.
*&---------------------------------------------------------------------*
*& Report zcl_material_details_consume
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zcl_material_details_consume.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
PARAMETERS P_MATNR TYPE MATNR.
SELECTION-SCREEN END OF BLOCK B1.
*Calling AMDP method.
ZCL_MATERIAL_DETAILS=>get_material_details(
EXPORTING
p_matnr = P_MATNR
IMPORTING
et_material = DATA(LT_MATERIAL)
).
*To display material details
CL_DEMO_OUTPUT=>display(
EXPORTING
data = LT_MATERIAL
name = 'MATERIAL DETAILS'
).
*& Report zcl_material_details_consume
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zcl_material_details_consume.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
PARAMETERS P_MATNR TYPE MATNR.
SELECTION-SCREEN END OF BLOCK B1.
*Calling AMDP method.
ZCL_MATERIAL_DETAILS=>get_material_details(
EXPORTING
p_matnr = P_MATNR
IMPORTING
et_material = DATA(LT_MATERIAL)
).
*To display material details
CL_DEMO_OUTPUT=>display(
EXPORTING
data = LT_MATERIAL
name = 'MATERIAL DETAILS'
).
- Save (Ctrl+S), activate (Ctrl+F3), and hit F8 for execution.
NOTE: By looking at the method definition, we can not judge whether its AMDP method or not. It's only when we see DATABASE PROCEDURE in method implementation, we can say that it's an AMDP method.
I hope the illustrations above help in better understanding the concept of AMDP.
In the final tutorial, we will learn about the term 'ALV with IDA'.
0 Comments