Codejam: Abap For Sap Hana 5 - Abap Managed Database Procedure (Amdp) and Consumption in Abap
Codejam: Abap For Sap Hana 5 - Abap Managed Database Procedure (Amdp) and Consumption in Abap
This document outlines our general product direction and should not be relied on in making a purchase decision. This
document is not subject to your license agreement or any other agreement with SAP. SAP has no obligation to pursue
any course of business outlined in this document or to develop or release any functionality mentioned in this
document. This document and SAP's strategy and possible future developments are subject to change and may be
changed by SAP at any time for any reason without notice. This document is provided without a warranty of any kind,
either express or implied, including but not limited to, the implied warranties of merchantability, fitness for a particular
purpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this document, except if such
damages were caused by SAP intentionally or grossly negligent.
2
Table of Contents
A. Why ABAP Managed Database Procedures (AMDP)? ...................................................................... 2
C.1 Create a first AMDP method for simple customer classification ....................................................... 5
C.2 Create a customer classification AMDP depending on The number of invoices ............................... 9
C.3 Pass SELECT-OPTION inputs to the AMDP method for filtering data set ........................................ 12
D. Summary ......................................................................................................................................... 15
With SAP NetWeaver 7.40 SP02 AS ABAP, there is a possibility to access so-called Stored Database
Procedures defined in SAP HANA via Database Procedure Proxies from the ABAP application server. From
a technical point of view, a stored procedure is written in SQLScript in SAP HANA and the corresponding
database procedure proxy (or proxies) can be generated in the ABAP server based on the interface
definition of the database procedure (Bottom-Up approach). The call of the procedure proxy in ABAP
triggers the procedure execution in SAP HANA. On the other side, the disadvantage of having a mix
between ABAP coding and database-specific coding is that those parts are managed by different lifecycle
management tools (ABAP and HANA), which need to be synchronized manually.
In contrast to this Bottom-Up approach, a Top-Down approach solves these synchronization issues,
because the database procedure coding is embedded within ABAP code, which is managed by the ABAP
lifecycle management tools and is executed at runtime on the database. Several technical realizations with
ABAP Database Connectivity (ADBC) and EXEC SQL are available since a longer time, but they are not
feasible to use in case of more complex scripts.
With SAP NetWeaver 7.40 SP05 AS ABAP there is a new way available to implement database procedures
within the ABAP stack as so-called ABAP Managed Database Procedures (AMDP). The procedure coding
is implemented directly in method container defined in a global ABAP class. Therefore, the corresponding
method needs to be marked as AMDP method in the implementation part of the class and the method body
contains database-specific script language coding like SQLScript instead of ABAP code, which is also
checked regarding syntactical correctness during implementation. Based on the script coding, a database
procedure is generated within SAP HANA once the new or changed AMDP method is executed in the
ABAP system (not at compile time).This allows the reuse of the ABAP transport infrastructure for these
database procedures, which do not need to be transported via the SAP HANA transport infrastructure.
From a technical point of view, AMDP methods have to be implemented in ABAP in Eclipse development
tool, exclusively.
3
Furthermore, for each AMDP method exactly one database procedure is generated in the database. This
allows for the protection of AMDP methods against unauthorized usage based on the ABAP package
concept as for any other classical ABAP method. This is not possible for SAP HANA stored procedures,
because several database procedure proxies can be created on the ABAP side to access a particular
stored procedure.
A class method can only be implemented as AMDP, if some prerequisites are fulfilled in the class definition
as well as in the class implementation part as depicted in the following figures.
Class definition
Class implementation
4
In this exercise you will familiarize yourself with the creation of AMDP methods and their consumption in
ABAP.
You will implement three different AMDP methods which return a result table with customer classifications
for selected customers. Customer classification means that customers are categorized, e.g. as premium,
standard or low-revenue customers, depending on certain categorization criteria.
The simplest classification implementation returns the same category for each customer, whereas the
second AMDP method the customer classification is based on the number of sales order invoices.
The exercises consist of the following main steps for each AMDP implementation:
· Create an ABAP method in your global class within ABAP developments tools
· Implement the customer classification in your AMDP method
· Display the output list of your classification implementation
Remark: This exercise does not aim to demonstrate the capabilities provided by SQLScript. Therefore the
AMDP methods implemented in this exercise are rather simple and use only SELECT statements.
This exercise introduces ABAP Managed Database Procedures (AMDP). It familiarizes you with the
definition of AMDP methods in ABAP classes and their implementation. All AMDP methods are
implemented with the database language SQLScript and declared as read-only, which allows the SAP
HANA database to optimize the procedure execution, if the procedure includes several SELECT statements
(complex logic). The following exercises do not implement complex procedure coding. They rather show a
few simple implementations with one SELECT, SELCT with JOIN, and a procedure execution, which
requires an intermediate result to calculate the customer classification.
A more complex AMDP implementation is available within the ABAP for HANA reference scenario “Open
Item Anlaysis (OIA)”. The class CL_OIA_CTG_PURE_AMDP_PRVDR is released with SAP NetWeaver 7.40
SP05 AS ABAP.
The following SQLScript Reference document provides the full capabilities of the SQLScript language in
SAP HANA, you can use in AMDP methods.
Content
Exercise C.1 - Create a first AMDP method for simple customer classification
Exercise C.2 - Create a customer classification AMDP depending on the number of invoices
Exercise C.3 - Pass SELECT-OPTION inputs to the AMDP method for filtering data set
5
Explanation Screenshot
PROTECTED SECTION.
PRIVATE SECTION.
Explanation Screenshot
4. Navigate to the implementation of
the classify_simple method
(Ctrl + left-mouse click) and
declare the method to be
implemented with SQLScript as
database procedure (BY
DATABASE PROCEDURE
statement).
METHOD classify_simple
BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY.
Explanation Screenshot
ENDMETHOD.
CASE iv_method .
WHEN zif_amdp_types_and_const=>co_classify_method-simple.
Explanation Screenshot
Info: In principle, you can implement interface methods as AMDP methods, if they fulfill the requirements
for implementing AMDP methods like the restriction to scalar or table-like parameters, BY VALUE
parameter transfer, etc.
A good programming style is to implement AMDP methods as private methods, which can be used only in
the class, in which they are implemented. A reuse / redefinition of public / protected AMDP methods in sub
classes by using ABAP code is in most cases not useful due to these restrictions. Nevertheless, this is
possible and therefore, if a suitable method contains ABAP code or SQLScript is decided within the method
implementation, and not within the method definition.
9
Implement an AMDP method, which returns a list of customers classified as “Premium”, “Standard” or “Low
revenue” customers depending on their number of invoices.
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
6. Pass the generated where clause
as parameter to the AMDP method.
Test running the report now.
Obviously the behavior of the
report would not have changed
since we have not made use of the
additional input parameter passed
to the AMDP method in its
implementation.
http://scn.sap.com/community/abap
/hana/blog/2015/03/30/handling-of-
select-options-parameters-within-
amdp
D. SUMMARY
You have implemented three different AMDP methods in global ABAP class. Each of these AMDP methods
contains an algorithm for a customer classification. You implemented the following customer classifications:
· All customers belong to the same category.
· Customers are classified by their number of invoices.
You have implemented an additional AMDP method that can accept the where clause corresponding to the
SELECT-OPTIONS from a ABAP report as input to restrict the result set retried from the database using the
APPLY_FILTER function.