Professional Documents
Culture Documents
ABAP Managed Database Procedures
ABAP Managed Database Procedures
So what are ABAP Managed Database Procedures? Similar to kernel methods, stored procedures
are encapsulated in special ABAP methods with a regular method interface but the body
implemented in SQLScript instead of ABAP.
The syntax for the implementation of this special ABAP method can be seen in the example below:
METHOD my_meth
BY DATABASE PROCEDURE FOR <db-type> LANGUAGE <db language>
[OPTIONS <options>]
[USING <names>].
-- Here comes native SQLScript now.
-- The SQLscript syntax rules apply,
-- e.g. '--' indicates a comment
-- the ABAP comment symbol >"< would lead to an error
ENDMETHOD.
Let’s talk about the additions seen in the syntax above.
•BY DATABASE PROCEDURE indicates, that this method does not carry ABAP code and will not
be executed in the ABAP Virtual Machine, but instead on the database level
•The obligatory addition FOR <db-type> defines the database type for which this stored procedure
is valid for. Right now, a database procedure can only be assigned for one DB vendor, and only
HANA (value "HDB") is supported.
•The mandatory addition LANGUAGE <db language> defines the implementation language for the
database procedure. Currently only SQLSCRIPT is allowed.
•The optional addition OPTIONS is used to declare database specific options for a database
procedure. The only option allowed at the moment is read-only, (which maps to the "reads sql
data" security privilege).
•USING <names>. All database objects that are used inside the method body, must be declared in
advance. This declaration is required for tables, views and any database procedures called within
the AMDP method.
One ABAP class can have multiple definitions of database procedures. The class can consist of
both ordinary ABAP methods and methods as database procedures.
AMDP methods can be called (from ABAP code) via the CALL METHOD statement, like any other
ABAP method.
Since not all aspects of ABAP class methods can be mapped one-by-one to database procedures,
several restrictions apply to the method definition of AMDP methods:
https://jam2.sapjam.com/wiki/show/rf327FFusTtlCMJEss7AtZ?print=true 06-12-2019
Page 2 of 6
PUBLIC SECTION.
ENDCLASS.
https://jam2.sapjam.com/wiki/show/rf327FFusTtlCMJEss7AtZ?print=true 06-12-2019
Page 3 of 6
As you know, ABAP classes are used to realize the concepts of object orientation in the ABAP
language. SQLScript is not an object oriented language, and so its important to note the following
points:
•Visibility control with PRIVATE/PROTECTED/PUBLIC.
ABAP classes support visibility control for methods, i.e. method X can only be called, if the caller Y
is allowed to do so. If the caller Y is implemented in ABAP, the full ABAP rules apply. Due to the
forward declaration (with the USING clause) of any procedures to be called, the static checks of the
ABAP compiler also apply to methods written in SQLScript, and so the visibility control of ABAP is
brought to the level of database procedures.
•Interfaces.
If a class X implements the interface Y, a method of Y can be realized as database procedure.
•Static and instance methods.
Both static as well as instance methods can be used as database procedures. (Although static
methods better fit to the semantics on the database level).
•Method redefinition.
A method M in class X can be redefined in class Y as a database procedure.
•The methods CONSTRUCTOR and CLASS-CONSTRUCTOR
Neither of these special methods can be defined as a Database Procedure.
Inside the AMDP method body, plain SQLScript syntax is allowed. This has the following
implications:
•The syntax check of the method body (i.e. the actual logic of a database procedure) is done on the
database level. So developing a database procedure for HANA on a system running on MaxDB is
like flying blind J
•There is no full translation of the SQLScript delimiters for statements or comments to the ABAP
environment. So in the method body,
• statements are to be delimited with <;>and not with <.> (as in ABAP)
• a comment to the line end is <-- >and not <"> (as in ABAP)
• There is one exception to this rule. The ABAP comment with<*> in the first column is
translated into a <-->comment on the database level. (However, it is recommended not to use
this kind of comment ).
Modularization
For many purposes it is sensible to split large database procedures into smaller units. ABAP
managed database procedures support modularization in the same way as you know it from the
ABAP language. The method METH of class CL_CLASS has the absolute name
CL_CLASS=>METH, hence this method can be called using
CALL METHOD CL_CLASS=>METH( ) from any other piece of ABAP code.
ABAP managed database procedures reuse the absolute ABAP name for the database procedure,
so if method CL_CLASS=>METH is tagged as a database procedure, a database procedure of
https://jam2.sapjam.com/wiki/show/rf327FFusTtlCMJEss7AtZ?print=true 06-12-2019
Page 4 of 6
exactly the same name will be created. The SQLScript statement CALL is then used to call the
database procedure with this name.
Example:
class zcl_amdp_example1 implementation.
method abc
by database procedure for hdb language sqlscript options read-only using table1 .
endmethod.
method xyz
by database procedure for hdb language sqlscript options read-only
using zcl_amdp_example1=>abc.
call "ZCL_AMDP_EXAMPLE1=>ABC"
( iv_parm1 => iv_parm1,
et_parm2 => et_parm2 );
endmethod.
endclass.
https://jam2.sapjam.com/wiki/show/rf327FFusTtlCMJEss7AtZ?print=true 06-12-2019
Page 5 of 6
message of this test creation is mapped back to the origin code position in the ABAP code,
providing the feeling of an integrated development environment.
However, not all features of the ABAP Editor are supported for ABAP managed database
procedures:
•Pretty printer support (Shift+F1)
The pretty printing logic will skip over all code that is marked as a database procedure.
•Forward navigation support (F3):
Currently there isn’t complete support for forward navigation from a caller database procedure to a
called database procedure.
•Where Used List (Crtl+Shift+G):
Not all callers of a called database procedure are found with the where used list functionality
From ABAP code to a Database Procedure - Generation of DB artefacts
Each method marked as a database procedure, will end up as a stored procedure at the database
level.
In addition, some other objects are created as well:
•Global temporary tables to support the transfer of table-like input and output parameters
•A view for database tables to ensure that "SELECT *" will work in SQLScript as expected from the
ABAP perspective
•A helper procedure ("stub") that ensures proper source code management and correct invocation
with named parameters of the actual database procedure
So how exactly is the database procedure created?
Mapping of the Method Definition Parts:
https://jam2.sapjam.com/wiki/show/rf327FFusTtlCMJEss7AtZ?print=true 06-12-2019
Page 6 of 6
Parameter mapping:
The parameters in the method definition are translated one-by-one to the procedure definition with
the following logic:
ABAP HANA
Importing in
Changing inout
Exporting out
For all table like parameters, a HANA datatype (transfer table) is created and used in the procedure
definition
All ABAP types are mapped to HANA types.
Source code mapping
In general the code between the ABAP statements METHOD and ENDMETHOD is passed mainly
unconverted into the procedure definition.
Transfer Tables
Transfer tables are used whenever table-like parameters are used.
•They act as a data container.
Since the database procedure invocation call from a client (such as HANA studio or the ABAP
server) doesn’t support table-like procedure parameters, a workaround has to be used. The content
of input parameters is pushed into the transfer table and the procedure is called with this content.
•They act as the type definition.
The transfer tables are technically "global temporary tables", which is the most suitable table type
for this operation. The ABAP runtime clears the tables after the procedure call. The handling of the
transfer tables is done in the same DB session as the actual procedure call.
The transfer tables are generated for each table-like parameter with the naming pattern [class
name]=>[method name]=>[parameter name]#tft.
https://jam2.sapjam.com/wiki/show/rf327FFusTtlCMJEss7AtZ?print=true 06-12-2019