Skip to Content
Author's profile photo Horst Keller

ABAP News for 7.40, SP08 – ABAP Managed Database Procedures (AMDP)

Recap

For code push down, meaning pushing analytics from the ABAP application server to the database server, you can use open ABAP features that run on any database. Those are Open SQL that was enhanced widely in ABAP 7.40 with analytic capabilities like SQL expressions or by removing restrictions from joins and the ABAP Core Data Services (ABAP CDS) for advanced view building in the ABAP Dictionary.

If the functionality offered by the open features is not sufficicient to cover your needs, you have to switch to Native SQL (sounds familiar?). This is especially true for SAP HANA, that offers analytical features not covered in standard SQL, as e.g. calculation views. If your ABAP programs should run on HANA  only (or if you want to offer special implementations for HANA and alternative implementations for other databases), database procedures written in SQLScript offer a convenient access to the capabilites of HANA. In order to facilitate this Native access to HANA, in 7.40, SP05 the concept of ABAP Managed Database Procedures (AMDP) was introduced, that allows you to code SQLScript in special methods of normal ABAP classes and the runtime environment does the rest for you. (But please note that a single SQL statement does not become faster if you code it in an AMDP, how should it? As long as you can stay open, stay open. There is no drawback in performance if you can express the logics in single statements or in CDS views. You use AMDP to avoid unnecessary data transfers between database and application server when using several statements or when you need access to fuctionality not covered by SQL).

News for ABAP 7.40, SP08

With ABAP 7.40, SP08, AMDP was enhanced as follows:

Tabular Changing Parameters

Although Native SQLScript procedures do not offer INOUT parameters, you can define CHANGING parameters for AMDP methods now. The trick is, that the database procedure generated from AMDP gets an additional IN parameter of the same name as the chaniging parameter with the pstfix __IN__ while the CHANGING parameter becomes an OUT parameter. When calling the procedure, the OUT parameter is filled implicitly with the CHANGING value passed to the IN parameter. Normally, you don’t have to care about the implicit IN parameter. Only if you want to call such a procedure from another database procedure, you have to supply it.

AMDP method definition

METHODS

  get_carriers

     CHANGING

       VALUE(carriers) TYPE t_carriers

     RAISING  cx_amdp_error.

AMDP method implementation

  METHOD get_carriers BY DATABASE PROCEDURE FOR HDB

                         LANGUAGE SQLSCRIPT

                      USING scarr.

    carriers  = select s.*

                     from scarr as s

                     inner join :carriers as c

                       on s.mandt  = c.mandt and

                          s.carrid = c.carrid;

  ENDMETHOD.

A good example where AMDP is not needed at all, but KISS here.

Call from ABAP

TRY.   

    NEW cl_demo_amdp_changing(

      )->get_carriers( CHANGING carriers = carriers ).

  CATCH cx_amdp_error INTO DATA(amdp_error).

    …

ENDTRY.

Class Based Exceptions

As shown above, you can handle exceptions that occur during processing an AMDP now. Before you got runtime errors only.

AMDP BAdIs

Special AMDP BAdIs allow you to implement BAdI methods as AMDP methods. These transfer the effect of the switches from Switch Framework to the implementation of database procedures in the current database. When an AMDP procedure calls another AMDP procedure managed by an AMDP BAdI, the implementation is executed that is prescribed by the current switch setting. (for calling AMDP procedures from ABAP no special BAdIs are necessary, because ABAP calls of normal BAdI methods are goverened by the switch framework anyway).

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Clemens Li
      Clemens Li

      Just a question. I got used to avoid ALIAS except for Self-joins for the sake of readability. Could I write


          carriers  = select scarr.*

                           from scarr

                           inner join :carriers

                             on scarr.mandt  = :carriers.mandt and

                                scarr.carrid = :carriers.carrid;

      for more clarity?


      And, btw, do I really need scarr.mandt  = :carriers.mandt as I thought mandt is implicitly supplied by the database interface?


      Thanks for enlightenment, I love this series!


      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      I checked, and your code doesn't compile.Well it is SQLScript and I'm not too much an expert there, but see http://help.sap.com/hana/sap_hana_sql_script_reference_en.pdf.

      Regarding client: It is Native SQL in full glory! No automatic client handling. Therfore, stay open as long as possible 😉 .

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      PS: CDS, ABAP News for 7.40, SP08 - ABAP Core Data Services (CDS), is open and clients are handled implicitly as in Open SQL, no client fields in the join there.

      Author's profile photo Paul Hardy
      Paul Hardy

      Hello Mr.Horst,

      In some SAP presentation on using SQLScript for HANA much is made of using CE Functions whenever possible as opposed to straight SELECT statments, as CE functions are supposed to run a lot faster, which makes up for them being not as flexible as SELECT statements.

      What I want to know, please, is if CE functions are just for when you are doing native development in the HANA studio (XS? River?) or can you use them in AMDP as well? I know you can use the CE_CURRENCY_CONVERSION as that is in a lot of the tutorials, but can you use others as well?

      And if it is in fact possible to use such things, does it make any sense in an ADMP enviroment? Is the recommendation still to use CE functions over SELECT statements in an ADMP? I know you should not mix them in the same procedure, as that would cause the universe to explode.

      As always, any help would be gratefully appreciated.

      Cheersy Cheers

      Paul

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      but can you use others as well

      Sure, the list of available functions can be taken from http://help.sap.com/hana/sap_hana_sql_script_reference_en.pdf

      does it make any sense in an ADMP enviroment

      Sure, AMDP facilitates this kind of code push down from ABAP programs. See the examples in my answer above. You write an AMDP procedure in order to call the function instead of having to use ADBC to access the functionality.

      Author's profile photo Former Member
      Former Member

      Hi

      nice document.

      I have created the following method

      METHODS retv_bgsml

              IMPORTING

                value(iv_from) type d

                value(iv_to) type d

                value(iv_plnt) TYPE tt_pnt

              exporting

                value(et_itm_bgsml) type tt_bgsml.

      in my zreport

      SELECT-OPTIONS s_plnt for z_cds_purc-plant

      lo_itm_sum->retv_bgsml(

        EXPORTING

          iv_from    = p_ivfrm

          iv_to      = p_ivto

         iv_plnt =  s_plnt[]

        IMPORTING

          et_itm_bgsml = DATA(lt_itm_sum)

      ).

      I get an error at iv_plnt it says splnt is not type compatible

      am i doing something wrong here.

      Thanks and regards

      Arun Kumar Menon

      Author's profile photo Clemens Li
      Clemens Li

      I think this has nothing in common with

      ABAP News for 7.40, SP08 - ABAP Managed Database Procedures (AMDP)

      please ask in ABAP forum.

      Hint: What is tt_pnt defined as?

      Regards

      Clemens