Skip to content

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

AMDP, Access to Database Schemas

Demonstrates access to tables in explicitly specified database schemas.

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_demo_output=>display(
        `Current database system does not support AMDP procedures` ).
      RETURN.
    ENDIF.

    TRY.
        cl_demo_amdp_db_schema=>get_schemas_physical(
          IMPORTING schemas = DATA(schemas_test) ).
      CATCH cx_amdp_error INTO DATA(amdp_error).
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    TRY.
        cl_demo_amdp_db_schema=>get_schemas_logical(
          IMPORTING schemas = DATA(schemas) ).
      CATCH cx_amdp_error INTO amdp_error.
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    ASSERT schemas_test = schemas.
    cl_demo_output=>write( schemas ).

    TRY.
        cl_demo_amdp_db_schema=>get_schemas_logical_to_abap(
          IMPORTING carriers = DATA(carriers) ).
      CATCH cx_amdp_error INTO amdp_error.
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    cl_demo_output=>display( carriers ).

Description

This example accesses AMDP methods that are declared and implemented in the AMDP class CL_DEMO_AMDP_DB_SCHEMA.

  • The method GET_SCHEMAS_PHYSICAL accesses the table SCHEMAS of the physical database schema SYS by specifying the schema directly. All existing database schema are saved in this table.
METHOD get_schemas_physical BY DATABASE PROCEDURE
                            FOR HDB LANGUAGE SQLSCRIPT.
  schemas =
    select schema_name
      FROM "SYS"."SCHEMAS";
ENDMETHOD.
METHOD get_schemas_logical BY DATABASE PROCEDURE
                           FOR HDB LANGUAGE SQLSCRIPT.
  schemas =
    select schema_name
      FROM "$ABAP.schema( DEMO_LOGICAL_DB_SCHEMA )"."SCHEMAS";
ENDMETHOD.
  • Another method, GET_SCHEMAS_LOGICAL_TO_ABAP, demonstrates how the addition USING SCHEMA of the statement METHOD is specified. The logical database schema DEMO_LOGICAL_DB_SCHEMA_TO_ABAP is defined in such a way that it allows access to the ABAP database schema. Since it is used in the method in the macro $ABAP.schema, it must be declared after USING SCHEMA.
METHOD get_schemas_logical_to_abap BY DATABASE PROCEDURE
                                   FOR HDB LANGUAGE SQLSCRIPT
                                   USING SCHEMA
                                     demo_logical_db_schema_to_abap
                                     OBJECTS scarr.
  carriers =
    select *
      FROM "$ABAP.schema( DEMO_LOGICAL_DB_SCHEMA_TO_ABAP )"."SCARR";
ENDMETHOD.
  • If the current ABAP database schema is mapped to the logical database schema using the predefined name :abap_db_schema, the full "$ABAP.schema( DEMO_LOGICAL_DB_SCHEMA_TO_ABAP  )"., if specified, is omitted when the macro is evaluated and the ABAP database schema is accessed implicitly. If specified, scarr is checked statically against the ABAP Dictionary.
  • If another physical database schema is mapped to the logical database schema, there must be a suitable database object SCARR in this database schema when the method is executed.