ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → AMDP - ABAP Managed Database Procedures → AMDP - Examples
AMDP, Implementation of an SQLScript Procedure
This example demonstrates how an SQLScript procedure is implemented using AMDP.
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.
DATA incprice TYPE sflight-price VALUE '0.1'.
DATA(client) = abap_false.
DATA(cds_client) = abap_false.
cl_demo_input=>new(
)->add_field( CHANGING
field = incprice
)->add_field( EXPORTING
as_checkbox = abap_true
text = `Use session variable CLIENT`
CHANGING
field = client
)->add_field( EXPORTING
as_checkbox = abap_true
text = `Use session variable CDS_CLIENT`
CHANGING
field = cds_client
)->request( ).
IF incprice IS INITIAL OR
client IS NOT INITIAL AND cds_client IS NOT INITIAL.
RETURN.
ENDIF.
SELECT price
FROM sflight
ORDER BY carrid, connid, fldate
INTO @DATA(price_before)
UP TO 1 ROWS.
ENDSELECT.
IF client IS INITIAL AND cds_client IS INITIAL.
TRY.
NEW cl_demo_amdp(
)->increase_price( clnt = sy-mandt
inc = incprice ).
CATCH cx_amdp_error INTO DATA(amdp_error).
cl_demo_output=>display( amdp_error->get_text( ) ).
RETURN.
ENDTRY.
ELSEIF client IS NOT INITIAL.
TRY.
NEW cl_demo_amdp(
)->increase_price_client( inc = incprice ).
CATCH cx_amdp_error INTO amdp_error.
cl_demo_output=>display( amdp_error->get_text( ) ).
RETURN.
ENDTRY.
ELSEIF cds_client IS NOT INITIAL.
TRY.
NEW cl_demo_amdp(
)->increase_price_cds_client( inc = incprice ).
CATCH cx_amdp_error INTO amdp_error.
cl_demo_output=>display( amdp_error->get_text( ) ).
RETURN.
ENDTRY.
ENDIF.
SELECT price
FROM sflight
ORDER BY carrid, connid, fldate
INTO @DATA(price_after)
UP TO 1 ROWS.
ENDSELECT.
IF price_after - price_before = incprice.
cl_demo_output=>display( `Price increased succesfully` ).
ENDIF.
Description
A simple SQLScript procedure is implemented in three AMDP methods (showing the different options for self-defined client handling) in the AMDP class CL_DEMO_AMDP:
- INCREASE_PRICE
LANGUAGE SQLSCRIPT
USING sflight.
update sflight set price = price + :inc
where mandt = :clnt;
ENDMETHOD.
clnt
is used to access the data of the passed client, as recommended.
- INCREASE_PRICE_CLIENT
LANGUAGE SQLSCRIPT
USING sflight.
update sflight set price = price + :inc
where mandt = SESSION_CONTEXT('CLIENT');
ENDMETHOD.
- INCREASE_PRICE_CDS_CLIENT
LANGUAGE SQLSCRIPT
USING sflight.
update sflight set price = price + :inc
where mandt = SESSION_CONTEXT('CDS_CLIENT');
ENDMETHOD.
AMDP OPTIONS CDS SESSION CLIENT CURRENT
is used for this purpose in the declaration of the method.
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 executable examples for
ADBC and for
CALL DATABASE PROCEDURE
. AMDP replaces these technologies when calling database procedures for an SAP HANA database that is the
standard 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 or CDS_CLIENT.
Note
This is a syntax example. The same function can be provided with the same efficiency in ABAP SQL. AMDP is not needed in simple cases like this.