Friday 25 November 2016

Implement and consume your first ABAP Managed Database Procedure on HANA

Prerequisites

  • SAP NetWeaver AS ABAP 7.4 Support Package 5 (or higher) running on SAP HANA
  • SAP HANA Appliance Software SPS 05 (or higher)
  • SAP HANA DB SQLScript V2.0 (or higher)
  • ABAP Development Tools for SAP NetWeaver

Tutorial Objectives

After completing this tutorial, you will be able to:

  • Declare an AMDP class
  • Declare an AMDP method
  • Implement an AMDP method
  • Consume an AMDP method in ABAP

Use Case Description

The Account Receivables accountant of your company want to be able to display the so-called top and flop customers in regards to their payment ability based on the gross amount of the open invoices.

The company accountant should be able to select how many customers have to be displayed per category and due to the regular update of the business data, the categorization have to be executed on-the-fly. Millions of open item invoices are typically processed in such tasks.

Procedure Overview

Implement and consume your first ABAP Managed Database Procedure on HANA

Step-by-Step Procedure

Step 1: Create an AMDP Class

1. In this step, you will create a regular global class and then mark it as AMDP class by specifying the tag interface for the SAP HANA database platform.
Start the ABAP Development Tools (aka ABAP in Eclipse) – or your SAP HANA Studio – and open the ABAP perspective by selecting menu entry Window > Open perspective > Others…, and choosing the ABAP entry  in the appearing dialog box.

2. Go to your ABAP project in the Project Explorer and create a new class in the package of your choice by selecting the context menu entry New… > ABAP Class
3. Maintain the required information (e.g. ZCL_OIA_TOPANDFLOP as name and “EPM OIA: Best and Worst Customers” as description) and click on Next.
Select a transport request if required and confirm the creation dialog.

Implement and consume your first ABAP Managed Database Procedure on HANA

4. Now go to the class definition and insert following line directly after PUBLIC SECTION:
      INTERFACESif_amdp_marker_hdb.
5. Save.

Implement and consume your first ABAP Managed Database Procedure on HANA

You’ve just created your first AMDP class! 

Step 2: Declare an AMDP Method

You will now declare the method get_top_and_flop which will be implemented as AMDP method later on. This method has an importing parameter for specifying the number of customers to be retrieved for each category (top and flop), and two exporting parameters getting for the two requested result sets.

Info: An AMDP class can contain both regular ABAP methods and AMDP methods. An AMDP is declared like a regular static method or instance method in any visibility section. An AMDP method cannot be identified as such in the declaration part of the class. Nevertheless, the parameter interface of an AMDP method has to fulfill specific prerequisites. For example the parameters must be declared using VALUE for pass by value  and return values cannot be declared using RETURNING.

Before going ahead with the method definition, we first have to defined the type of the result sets.

1. Define the two types ty_bupa_selection and tt_bupa_selection in the PUBLIC SECTION of the class definition – With ty_bupa_selection defining the table line of our return set, and tt_bupa_selection defining the type of the returned tables.
For that just copy the below coding after the interface declaration done in the previous steps:

TYPES:
  BEGIN OF ty_bupa_selection,
    company_name TYPE c LENGTH 80,
    gross_amount TYPE p LENGTH 8 DECIMALS 2,
  END OF ty_bupa_selection.

TYPES:
  tt_bupa_selection TYPE STANDARD TABLE OF ty_bupa_selection WITH EMPTY KEY.

2. Now define the public static method get_top_and_flop.
Just copy and pase the coding below directly after the type definitions in the public section:

CLASS-METHODS get_top_and_flop
  IMPORTING
         VALUE(iv_client)  TYPE mandt
    VALUE(iv_number) TYPE i
  EXPORTING
    VALUE(et_top)    TYPE tt_bupa_selection
    VALUE(et_flop)   TYPE tt_bupa_selection.


Info: Analog to native SQL with ADBC and EXEC, the AMDP framework does not support automatic client handling. It means that in case of client-specific computations, the client context has to be passed to the AMDP method and used appropriately in the SQLScript coding.

3. An error will be displayed in the editor due to the missing method implementation. Just use the Quick Fix (Ctrl+1) function “Add implementation for get_top_and_flop” to quickly solved this issue.

4. Save your AMDP class.

Step 3: Implement the AMDP Method

You will now implement a relatively simple SQLScript-based AMDP method, which retrieves the best and worst customers depending on the gross amount of open invoices.

Info: Whether a method is implemented as ABAP or as AMDP method is not decided in the class definition, but rather in the class implementation.

An AMDP method is indicated as an AMDP method in the implementation part of the class using the addition BY DATABASE PROCEDURE of the statement METHOD. At the same time, the database platform where the method is used and the programming language used to implement the method are respectively specified with the additions FOR and LANGUAGE. Further additions are available.

1. Mark the method implementation as an AMDP method.
Go to the class definition and enhance the method with the required additions as displayed below:

METHOD get_top_and_flop BY DATABASE PROCEDURE
                          FOR HDB
                          LANGUAGE SQLSCRIPT
                          OPTIONS  READ-ONLY
USING snwd_so_i snwd_so snwd_bpa.

ENDMETHOD.

The compiler now knows, that the implementation of the method get_top_and_flop of the class ZCL_OIA_TOPANDFLOP is an SQLScript-based AMDP for the HANA database platform.The addition USING contains the name of the DDIC tables which will be used in the implementation.

2. Now implement the database procedure by copying the SQLScript source below

–retrieve the best customers
et_top = select top :iv_number bp.company_name as company_name, sum(soi.gross_amount) as gross_amount
          from snwd_so_i as soi
          inner join snwd_so  as so
on so.node_key = soi.parent_key and so.client = soi.client
          inner join snwd_bpa as bp
            on bp.node_key = so.buyer_guid and bp.client = so.client
          group by company_name
          order by gross_amount desc;

–retrieve the worst customers
et_flop = select top :iv_number bp.company_name as company_name, sum(soi.gross_amount) as gross_amount
          from snwd_so_i as soi
            inner join snwd_so  as so
on so.node_key = soi.parent_key and so.client = soi.client
            inner join snwd_bpa as bp
              on bp.node_key = so.buyer_guid and bp.client = so.client
            group by company_name
            order by gross_amount asc;

Note: The purpose of this tutorial is not to introduce the SQLScript programming language or to demonstrate how complex the logic of a database procedure can be. The above procedure implements a relatively simple data-intensive function, but of course very complex logic (making even use of the advanced HANA features and functions such as data mining and predictive analysis) can be implemented.

As already stated, in case of client-specific computation, the client context has to be passed to the AMDP method and used appropriately in the SQLScript coding. 

Info: In order to quickly visualize whether a class contains AMDP methods and where, it is recommended to set a different background color for embedded languages – such as native SQL and SQLScript.
To achieve this, go to the ADT menu entry Windows > Preferences and select the path General > Appearance > Color and Fonts > ABAP > Syntax Coloring > Embedded Languages (background color) and set the background color of your choice.

3. Save and activate your AMDP class.

You’re now ready to test your AMDP method!

Implement and consume your first ABAP Managed Database Procedure on HANA

Step 4: Create an ABAP Report consuming the AMDP method

We will now create and implement a simple ABAP report which call the AMDP method and display the result sets on the screen.

Info: An AMDP method is called like any other method in ABAP Objects. This requires, however, that the central database of the current AS ABAP is managed by the database system for which the AMDP method is implemented – meaning SAP HANA in our case. If not, a runtime error is produced. Detailed analysis of such error can be done using the tools of the ABAP Dump Analysis (ST22).

1. Create the ABAP program ZR_OIA_TOPANDFLOP.
Select the package of your choice, right-click on it and choose context menu entry New > ABAP Program.

Enter the required information (name, a description – e.g. “Retrieve and Display Sales Order with Open Days and BuPa Data”-) and press Next.

Select a transport request if required and press Finish to confirm the creation dialog.

2. Now implement the report.
For this purpose, just copy & paste the source code below into the ABAP editor.

PARAMETER pnumber TYPE i DEFAULT 10.

DATA: lv_number TYPE  i.

* set the value of the procedure input parameter
lv_number = pnumber.
* call  AMDP methods
zcl_oia_top_and_flop=>get_top_and_flop(
                EXPORTING iv_number = lv_number
iv_client = sy-mandt
                IMPORTING et_top = data(lt_top)
                          et_flop = data(lt_flop) ).
* display the returned itab with TOP customers
WRITE: / ‘Best customers:’ COLOR COL_POSITIVE.
LOOP AT lt_top ASSIGNING FIELD-SYMBOL(<f>).
  WRITE:/ <f>-company_name , <f>-gross_amount.
ENDLOOP.
* display the returned itab with FLOP customers
ULINE.
WRITE: ‘ Worst customers:’ COLOR COL_NEGATIVE.
LOOP AT lt_flop ASSIGNING FIELD-SYMBOL(<g>).
  WRITE:/ <g>-company_name , <g>-gross_amount .
ENDLOOP.

3. Save and activate your test report.

4. You can now run the report (press F8) and see the result of your effort.

Implement and consume your first ABAP Managed Database Procedure on HANA

No comments:

Post a Comment