Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  AMDP - ABAP Managed Database Procedures →  AMDP - Examples 

AMDP, AMDP Functions

This example demonstrates AMDP functions and how they are used.

Other versions: 7.31 | 7.40 | 7.54

Source Code

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

    "AMDP table 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.

    "AMDP table 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=>write( result1 ).

    "AMDP scalar function called from ABAP
    TRY.
        SELECT carrid, connid, cityfrom, cityto, fltime
               FROM spfli
               WHERE carrid = @carrid AND
                     fltime = @( NEW cl_demo_amdp_functions_inpcl(
                               )->get_max_fltime_spfli(
                                    EXPORTING clnt = sy-mandt
                                              carrid = carrid ) )
               INTO TABLE @DATA(result3).
      CATCH cx_amdp_error INTO amdp_error.
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    "AMDP scalar function called from database procedure
    TRY.
        NEW cl_demo_amdp_functions_inpcl(
              )->select_spfli_by_max_fltime(
          EXPORTING clnt   = sy-mandt
                    carrid = carrid
          IMPORTING spfli_tab = DATA(result4) ).
      CATCH cx_amdp_error INTO amdp_error.
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    ASSERT result3 = result4.
    cl_demo_output=>display( result3 ).

Description

This example accesses 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.
This method cannot be called in ABAP.
  • The method SELECT_GET_SCARR_SPFLI is an AMDP procedure implementation that uses SELECT to access the AMDP table function implemented in the method GET_SCARR_SPFLI.
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.
This method is called in the example program.
@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 the CDS table function.
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.
The example program uses SELECT to access the CDS table function. The input parameter is filled here. Access to both AMDP table functions produces the same result, which is ensured by the ASSERT statement.
  • The method GET_MAX_FLTIME_SPFLI is an AMDP function implementation for an AMDP scalar function. It has an explicitly declared interface and can be called in ABAP.
METHOD get_max_fltime_spfli
       BY DATABASE FUNCTION FOR HDB
       LANGUAGE SQLSCRIPT
       OPTIONS READ-ONLY
               DETERMINISTIC
       USING spfli.
  SELECT MAX(fltime) INTO max_fltime
               FROM spfli
                 WHERE mandt = :clnt AND
                       carrid = :carrid;
ENDMETHOD.
In the example program, a WHERE condition of a SELECT statement contains a host expression with a functional method call of this ADMP function implementation.
  • The method SELECT_SPFLI_BY_MAX_FLTIME is an AMDP procedure implementation where the AMDP scalar function that is implemented in the method GET_MAX_FLTIME_SPFLI is called in its SELECT statement in the WHERE condition.
METHOD select_spfli_by_max_fltime
       BY DATABASE PROCEDURE FOR HDB
       LANGUAGE SQLSCRIPT
       OPTIONS READ-ONLY
       USING spfli
             cl_demo_amdp_functions_inpcl=>get_max_fltime_spfli.
  spfli_tab =
  select carrid, connid, cityfrom, cityto, fltime
         from spfli
              where
                mandt = :clnt and
                fltime =
                "CL_DEMO_AMDP_FUNCTIONS_INPCL=>GET_MAX_FLTIME_SPFLI"(
                  clnt => :clnt,
                  carrid => :carrid );
ENDMETHOD.
This method is called in the example program. The preceding ABAP SQL statement and the method call have the same result, which is ensured by the ASSERT statement.


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 ABAP 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
@AccessControl.authorizationCheck:#NOT_ALLOWED
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;