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

AMDP, AMDP Functions

This example demonstrates AMDP functions and how they are used.

Source Code

REPORT demo_amdp_functions_inpcl.

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

CLASS demo IMPLEMENTATION.
  METHOD main.
    IF NOT cl_abap_dbfeatures=>use_features(
          EXPORTING
            requested_features =
              VALUE #( ( cl_abap_dbfeatures=>call_amdp_method )
                       ( cl_abap_dbfeatures=>amdp_table_function ) ) ).
      cl_demo_output=>display(
        `System does not support AMDP or CDS table functions` ).
      RETURN.
    ENDIF.

    DATA carrid TYPE s_carr_id VALUE 'LH'.
    cl_demo_input=>request( CHANGING field = carrid ).
    carrid = to_upper( carrid ).

    "Database function selected in database procedure
    TRY.
        NEW cl_demo_amdp_functions_inpcl( )->select_get_scarr_spfli(
          EXPORTING clnt   = sy-mandt
                    carrid = carrid
          IMPORTING scarr_spfli_tab = DATA(result1) ).
      CATCH cx_amdp_error INTO DATA(amdp_error).
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    "Database function selected via CDS entity
    SELECT *
           FROM demo_cds_get_scarr_spfli_inpcl( carrid = @carrid )
           INTO TABLE @DATA(result2)
           ##db_feature_mode[amdp_table_function].

    ASSERT result1 = result2.
    cl_demo_output=>display( result1 ).

  ENDMETHOD.
ENDCLASS.

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

Description

This example access AMDP functions that are declared and implemented in the AMDP class CL_DEMO_AMDP_FUNCTIONS_INPCL.

METHOD get_scarr_spfli BY DATABASE FUNCTION FOR HDB
                       LANGUAGE SQLSCRIPT
                       OPTIONS READ-ONLY
                       USING scarr spfli.
  RETURN SELECT sc.carrname, sp.connid, sp.cityfrom, sp.cityto
                FROM scarr AS sc
                  INNER JOIN spfli AS sp ON sc.mandt = sp.mandt AND
                                            sc.carrid = sp.carrid
                  WHERE sp.mandt = :clnt AND sp.carrid = :carrid
                  ORDER BY sc.mandt, sc.carrname, sp.connid;

ENDMETHOD.
METHOD select_get_scarr_spfli
       BY DATABASE PROCEDURE FOR HDB
       LANGUAGE SQLSCRIPT
       OPTIONS READ-ONLY
       USING cl_demo_amdp_functions_inpcl=>get_scarr_spfli.
  SCARR_SPFLI_TAB =
  SELECT *
         FROM "CL_DEMO_AMDP_FUNCTIONS_INPCL=>GET_SCARR_SPFLI"(
                clnt => :clnt,
                carrid => :carrid );
ENDMETHOD.
@ClientHandling.type: #CLIENT_DEPENDENT
define table function DEMO_CDS_GET_SCARR_SPFLI_INPCL
  with parameters
    @Environment.systemField: #CLIENT
    clnt   :abap.clnt,
    carrid :s_carr_id
  returns
  {
    client   :s_mandt;
    carrname :s_carrname;
    connid   :s_conn_id;
    cityfrom :s_from_cit;
    cityto   :s_to_city;
  }
  implemented by method
    CL_DEMO_AMDP_FUNCTIONS_INPCL=>GET_SCARR_SPFLI_FOR_CDS;
This method is declared using the addition FOR TABLE FUNCTION and its interface is determined using theCDS table function. The example program uses SELECT to access the CDS table function. The input parameter is filled here.
METHOD get_scarr_spfli_for_cds
       BY DATABASE FUNCTION FOR HDB
       LANGUAGE SQLSCRIPT
       OPTIONS READ-ONLY
       USING scarr spfli.
  RETURN SELECT sc.mandt as client,
                sc.carrname, sp.connid, sp.cityfrom, sp.cityto
                FROM scarr AS sc
                  INNER JOIN spfli AS sp ON sc.mandt = sp.mandt AND
                                            sc.carrid = sp.carrid
                  WHERE sp.mandt = :clnt AND
                        sp.carrid = :carrid
                  ORDER BY sc.mandt, sc.carrname, sp.connid;

ENDMETHOD.

Both accesses produce the same result (verified by the statement ASSERT).

Note

The CDS table function DEMO_CDS_GET_SCARR_SPFLI_INPCL used here has one input parameter for the client, annotated using the annotation @Environment.systemField and the predefined value #CLIENT. This input parameter is filled explicitly with the ID of the current client by the Open SQL statement SELECT and used in the implementation of the associated AMDP method for selecting the data. The nearly identical program DEMO_AMDP_FUNCTIONS uses the following CDS table function DEMO_CDS_GET_SCARR_SPFLI in which the current client is selected from the results set of the table function.

@ClientHandling.type: #CLIENT_DEPENDENT
define table function DEMO_CDS_GET_SCARR_SPFLI
  with parameters
    carrid :s_carr_id
  returns
  {
    client   :s_mandt;
    carrname :s_carrname;
    connid   :s_conn_id;
    cityfrom :s_from_cit;
    cityto   :s_to_city;
  }
  implemented by method
    CL_DEMO_AMDP_FUNCTIONS=>GET_SCARR_SPFLI_FOR_CDS;