ABAP - Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Native SQL →  AMDP - ABAP Managed Database Procedures →  AMDP - Examples → 

AMDP, Implementation of a SQLScript Procedure

This example demonstrates how an SQLScript procedure is implemented using AMDP.

Source Code

REPORT demo_amdp.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA clnt         TYPE abap_bool.
    DATA incprice     TYPE sflight-price.
    DATA price_before TYPE sflight-price.
    DATA price_after  TYPE sflight-price.

    IF NOT cl_abap_dbfeatures=>use_features(
          EXPORTING
            requested_features =
              VALUE #( ( cl_abap_dbfeatures=>call_amdp_method ) ) ).
      cl_demo_output=>display(
        `Current database system does not support AMDP procedures` ).
      RETURN.
    ENDIF.

    cl_demo_input=>add_field( CHANGING
                                field = incprice ).
    cl_demo_input=>add_field( EXPORTING
                                as_checkbox = abap_true
                                text        = `Use session client`
                               CHANGING
                                 field = clnt ).
    cl_demo_input=>request( ).

    IF incprice IS INITIAL.
      RETURN.
    ENDIF.

    SELECT SINGLE price FROM sflight INTO price_before.

    IF clnt = abap_true.
      TRY.
          NEW cl_demo_amdp( )->increase_price( inc  = incprice ).
        CATCH cx_amdp_error INTO DATA(amdp_error).
          cl_demo_output=>display( amdp_error->get_text( ) ).
          RETURN.
      ENDTRY.
    ELSE.
      TRY.
          NEW cl_demo_amdp( )->increase_price_clnt( clnt = sy-mandt
                                                    inc  = incprice ).
        CATCH cx_amdp_error INTO amdp_error.
          cl_demo_output=>display( amdp_error->get_text( ) ).
          RETURN.
      ENDTRY.
    ENDIF.


    SELECT SINGLE price FROM sflight INTO price_after.
    IF price_after - price_before = incprice.
      cl_demo_output=>display( `Price increased succesfully` ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

A simple SQLScript procedure is implemented in two AMDP methods in the AMDP class CL_DEMO_AMDP:

METHOD increase_price BY DATABASE PROCEDURE FOR HDB
                        LANGUAGE SQLSCRIPT
                        USING sflight.
  update sflight set price = price + :inc
                 where mandt = SESSION_CONTEXT('CLIENT');
ENDMETHOD.
In this implementation, the ABAP-specific session variable CLIENT is used to access the data of the current client.
METHOD increase_price_clnt BY DATABASE PROCEDURE FOR HDB
                           LANGUAGE SQLSCRIPT
                           USING sflight.
  update sflight set price = price + :inc
                 where mandt = :clnt;
ENDMETHOD.
In this implementation, an explicitly passed input parameter CLNT is used to access the data of the passed client.

The database table SPFLI defined in ABAP Dictionary must be specified after USING. On a SAP HANA database, this program works in the same way as the examples for ADBC and for CALL DATABASE PROCEDURE. AMDP replaces these technologies when calling database procedures for an SAP HANA database that is the central database of an AS ABAP.

It is generally preferable to pass the client ID to an input parameter of a use of the ABAP-specific session variable CLIENT (see Client Handling).

Note

This is a syntax example. The same function can be provided with the same efficiency in Open SQL. AMDP is not needed in simple cases like this.