0% found this document useful (0 votes)
616 views15 pages

Codejam: Abap For Sap Hana 5 - Abap Managed Database Procedure (Amdp) and Consumption in Abap

Here is the code to declare the classify_simple method as an AMDP method implemented with SQLScript: METHOD classify_simple. BY DATABASE PROCEDURE. ENDMETHOD. 5. Implement the simple customer classification logic in SQLScript within the method body. All customers should get the same category 'STANDARD'. Return the result as table ET_CLASSIFICATION. The SQLScript code should look like: METHOD classify_simple. BY DATABASE PROCEDURE. SELECT * FROM ( VALUES ('STANDARD') ) AS T(CLASSIFICATION) INTO @ET_CLASSIFICATION. ENDMETHOD. This implements a simple

Uploaded by

Jose Valencia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
616 views15 pages

Codejam: Abap For Sap Hana 5 - Abap Managed Database Procedure (Amdp) and Consumption in Abap

Here is the code to declare the classify_simple method as an AMDP method implemented with SQLScript: METHOD classify_simple. BY DATABASE PROCEDURE. ENDMETHOD. 5. Implement the simple customer classification logic in SQLScript within the method body. All customers should get the same category 'STANDARD'. Return the result as table ET_CLASSIFICATION. The SQLScript code should look like: METHOD classify_simple. BY DATABASE PROCEDURE. SELECT * FROM ( VALUES ('STANDARD') ) AS T(CLASSIFICATION) INTO @ET_CLASSIFICATION. ENDMETHOD. This implements a simple

Uploaded by

Jose Valencia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CODEJAM: ABAP FOR SAP HANA

5 – ABAP MANAGED DATABASE PROCEDURE


(AMDP) AND CONSUMPTION IN ABAP

June 2015 – SAP SE

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

B. What’s in this exercise? .................................................................................................................... 4

C. Create AMDP methods for customer classification............................................................................ 4

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

A. WHY ABAP MANAGED DATABASE PROCEDURES (AMDP)?

Significant performance improvements of data-intensive processes can be achieved by a code push-down


from the application server to the database server. This usually reduces the number of data transfers and
the amount of transferred data between both servers.

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

B. WHAT’S IN THIS EXERCISE?

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.

C. CREATE AMDP METHODS FOR CUSTOMER CLASSIFICATION

Estimated total time: 60 minutes

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

C.1 CREATE A FIRST AMDP METHOD FOR SIMPLE CUSTOMER CLASSIFICATION

Estimated time: 20 minutes

Explanation Screenshot

1. Make sure you’re in the ABAP


perspective of your Eclipse IDE
and open your development
package TEST_A4H_EX_##
(where ## represents your group
number) and double click on the
class ZCL_AMDP_CLASSIFY_##
to open the class

2. Include the AMDP marker interface


IF_AMDP_MARKER_HDB in the
public section of the class
definition.

3. Define the parameter interface of


the method classify_simple for
a usage as AMDP method.
You have to transfer a list of
customers for the classification and
the client parameter into the AMDP
method. The method returns the
customer classification as
exporting parameter.

Code: Define the parameter interface of classify_simple


PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
INTERFACES zif_amdp_cust_classification.

PROTECTED SECTION.
PRIVATE SECTION.

* AMDP Exercise C1: enhance parameter interface for simple classification:


METHODS classify_simple
IMPORTING VALUE(it_bp_id) TYPE zif_amdp_types_and_const=>tt_bp_id
VALUE(iv_client) TYPE sy-mandt
EXPORTING VALUE(et_classification) TYPE zif_amdp_types_and_const=>tt_bp_classification.
6

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).

Keep in mind, so far there is no


SQLScript coding within the
method body, you will find some
error messages from the syntax
check.

Code snippet: Declare classify_simple as AMDP


CLASS zcl_amdp_classify_sqlscript_00 IMPLEMENTATION.

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

5. Implement the method


classify_simple as database
procedure.

You select the required customers


from database table SNWD_BPA
with the ID and the company name,
and you classify all customers as
“Standard” customers using the
category code value 3 and
category text “Standard”.
The columns category and
category_txt of the table
et_classification can be
filled directly with fixed values and
literals.

6. All database tables (and reused


ABAP managed database
procedures) within the procedure
coding must be declared in the
USING clause of the method
declaration.
Add the table SNWD_BPA to the
method declaration.
7

Explanation Screenshot

Code snippet: Implement classify_simple as AMDP


METHOD classify_simple
BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY
USING snwd_bpa.

* AMDP Exercise C1:


* read customer and set "Standard" classification category:
et_classification = SELECT bp_id,
company_name,
3 AS category,
'Standard' as category_txt
FROM snwd_bpa
WHERE client = :iv_client
AND bp_id IN ( select bp_id from :it_bp_id )
ORDER BY bp_id, company_name;

ENDMETHOD.

7. Complete the implementation of


the interface method classify of
interface
ZIF_AMDP_CUST_CLASSIFICATION
for the call of the AMDP method
classifiy_simple

Code snippet: Complete zif_amdp_cust_classification~classify


METHOD zif_amdp_cust_classification~classify.

* AMDP Exercise C1:


DATA lt_bp_id TYPE zif_amdp_types_and_const=>tt_bp_id.

SELECT bp_id FROM snwd_bpa INTO CORRESPONDING FIELDS OF TABLE lt_bp_id


WHERE company_name IN it_cp_nam_rg.

CASE iv_method .

WHEN zif_amdp_types_and_const=>co_classify_method-simple.

* AMDP Exercise C1:


classify_simple( EXPORTING it_bp_id = lt_bp_id
iv_client = sy-mandt
IMPORTING et_classification = et_classification ).

8. Save and activate your changes.


8

Explanation Screenshot

9. Test your class with report


ZR_AMDP_CUST_CLASSIFICATION.
Select it from the package
TEST_A4H_SOLUTIONS and press
F8. This opens a SAPGUI window
in the HANA Studio showing the
selection screen for the customer
classification. First switch on the
MyClass checkbox and afterwards
select your implementation from
the value help of field CLASS.
Select parameter METHOD from
value help list with value 1 and
provide a customer range, for
which the classification is
determined.

10. Execute the report (F8) and


check the result. All your
customers must have the category
code value 3.

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

C.2 CREATE A CUSTOMER CLASSIFICATION AMDP DEPENDING ON THE NUMBER OF


INVOICES

Implement an AMDP method, which returns a list of customers classified as “Premium”, “Standard” or “Low
revenue” customers depending on their number of invoices.

Estimated time: 20 minutes

Explanation Screenshot

1. Open your package


TEST_A4H_EX_## (where ##
represents your group number)
and double click on the class
ZCL_AMDP_CLASSIFY_## to
open the class

2. Define the parameter interface of


the method
classify_by_invc for a
usage as AMDP method.
You have to transfer a list of
customers for the classification,
the client parameter and the min
and max limits for the number of
invoices into the AMDP method.
The method returns the
customer classification as
exporting parameter.
Code snippet: Define the parameter interface of classify_by_invc
* AMDP Exercise C3: enhance parameter interface for classification by invoices assignment:
METHODS classify_by_invc
IMPORTING VALUE(it_bp_id) TYPE zif_amdp_types_and_const=>tt_bp_id
VALUE(iv_client) TYPE sy-mandt
VALUE(iv_min) TYPE i
VALUE(iv_max) TYPE i
EXPORTING VALUE(et_classification) TYPE zif_amdp_types_and_const=>tt_bp_classification.

3. Navigate to the implementation


of the classify_by_invc
method and declare the method
to be implemented with
SQLScript as AMDP (BY
DATABASE PROCEDURE
statement).
10

Explanation Screenshot

4. Implement the method


classify_by_invc as database
procedure. You select the
required customers from
database table SNWD_BPA with
the ID and the company name,
but you need to join this table
with SNWD_SO_INV_HEAD to
classify the customers based on
their number of invoices.

Code snippet: Implement classify_by_invc as AMDP


METHOD classify_by_invc
BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY
USING snwd_so_inv_head
snwd_bpa.

* get number of invoice per customer:


lt_inv_count = SELECT bpa.bp_id as bp_id,
bpa.company_name as company_name,
COUNT(inv.node_key) as invoice_number
FROM snwd_bpa as bpa
INNER JOIN snwd_so_inv_head as inv ON inv.client = bpa.client
AND inv.buyer_guid = bpa.node_key
WHERE bpa.client = :iv_client
AND bpa.bp_id IN ( select bp_id from :it_bp_id )
GROUP BY bpa.bp_id, bpa.company_name;

* replace invoice counter by classification code for customer


et_classification = SELECT bp_id,
company_name,
CASE
WHEN invoice_number < :iv_min THEN 1 --low revenue
WHEN invoice_number > :iv_max THEN 2 --premium
ELSE 3 --standard
END AS category,
CASE
WHEN invoice_number < :iv_min THEN 'Low revenue'
WHEN invoice_number > :iv_max THEN 'Premium'
ELSE 'Standard'
END AS category_txt
FROM :lt_inv_count
ORDER BY bp_id, company_name;
ENDMETHOD.
11

Explanation Screenshot

5. Complete the implementation of


the interface method classify of
interface
ZIF_AMDP_CUST_CLASSIFICATION
for the call of the AMDP method
classifiy_by_invc

Code snippet: Complete zif_amdp_cust_classification~classify


WHEN zif_amdp_types_and_const=>co_classify_method-by_invoice.
* AMDP Exercise C3:
classify_by_invc(
EXPORTING it_bp_id = lt_bp_id
iv_client = sy-mandt
iv_min = zcl_amdp_cust_classify_assist=>gv_min
iv_max = zcl_amdp_cust_classify_assist=>gv_max
IMPORTING et_classification = et_classification ).
6. Activate the changes in the class

7. Test your class with report


ZR_AMDP_CUST_CLASSIFICATION.
Select it from the package
TEST_A4H_SOLUTIONS and
press F8. This opens a SAPGUI
window in the HANA Studio
showing the selection screen for
the customer classification. First
switch on the MyClass checkbox
and afterwards select your
implementation from the value
help of field CLASS. Select
parameter METHOD from value
help list with value 3 and provide
a customer range, for which the
classification is determined.

8. Execute the report (F8) and


check the result. Either the
customers are classified as
Premium (value 3), as Standard
(value 2) or Low revenue
(value 1) customers depending
on their number of invoices.
12

C.3 PASS SELECT-OPTION INPUTS TO THE AMDP METHOD FOR FILTERING


DATA SET
Analyze the existing ABAP Report which consumes an AMDP method which calculates and returns the list
of customers with open invoices. The returned list contains details about each customer .In addition the
returned result also contains details like average numbers days the invoices are open for the customer and
total outstanding invoice amount for all the open invoices (converted to a target currency).
Nevertheless the AMDP implementation has one limitation that it does the computation for all the customers
and there is no possibility of receiving the results only for select customers from the method call.
The report has selection screen options to receive the user’s input on which customers the result has to be
calculated.
In this exercise we will do the following
1. Enhance the AMDP method definition to receive the inputs on selected customers as an additional
input value.
2. Modify the existing AMDP implementation to restrict the result set based on the new input
parameter.
3. Convert the selection screen inputs into a “where” clause string using a special ABAP utility class
method.
4. Modify the method call from the report by additionally passing the “where” clause string created
using the ABAP utility method call in the previous step

Estimated time: 20 minutes

Explanation Screenshot

1. Understand the coding in the


report
ZR_AMDP_OPEN_AMOUNT_EX
_## .Open your package
TEST_A4H_EX_## (where ##
represents your group number)
and double click on the report
ZR_AMDP_OPEN_AMOUNT_EX
_## to open the report.

2. The report calls method


GET_OPEN_AMOUNTS of the
class
ZCL_AMDP_OPEN_AMOUNT_E
X_##.
Navigate to the implementation of
the method
GET_OPEN_AMOUNTS using
the short cut key F3 from the
method call
13

Explanation Screenshot

3. Run the report and check the


behavior by passing values for
BUPA_ID and
CUSTOMER_NAME. The output
of the report run shows open
invoice details of all the
customers even though a specific
BP_ID is chosen in the selection
screen.

4. Analyze how the selection screen


options for BUPA_ID and
CUSTOMER_NAME fields are
handled. The report currently does
not make use of the selection
screen inputs.
Modify the signature of the AMDP
Method in class
ZCL_AMDP_OPEN_AMOUNT with
an additional input parameter
IV_WHERE type STRING.

5. Modify report implementation to


use the method
CL_SHDB_SELTAB=>COMBINE_
SELTABS
This method accepts the SELECT-
OPTIONS fields and the
corresponding where clause field
name as input and return a where
clause string with the condition
populated
14

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

7. Modify the AMDP method


implementation such that the final
result is assigned from the output
of the APPLY_FILTER method with
the intermediate result and the
where clause string as inputs
parameters.

8. Run the modified report and check


the behavior by passing values for
BUPA_ID and
CUSTOMER_NAME. The output of
the report run shows open invoice
details for only the customers
chosen in the selection screen. If
no selection screen inputs are
provided the result is shown for all
customers.
15

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.

You are now able to:


· prepare a global class for implementing AMDP methods
· define suitable methods in a global ABAP class for usage as AMDP implementation
· declare an AMDP method in the class implementation using the addition
BY DATABASE PROCEDURE
FOR db
LANGUAGE db_lang
OPTIONS db_options
USING db_entities
· implement simple read-only AMDP method accessing database tables using a SELECT statement
· implement AMDP method that can accept values from SELECT-OPTIONS as WHERE clause for
result set filtering and restriction.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy