100% found this document useful (1 vote)
1K views73 pages

ABAP Managed Database Procedure

ABAP Managed Database Procedures (AMDP) allow developers to call stored procedures in the database from ABAP. AMDP procedures are implemented as methods in AMDP classes using database-specific languages like SQL Script. AMDP supports polymorphism and calling procedures between each other. It integrates with BAdIs and requires authorizations to manage database procedures. AMDP should only be used when Open SQL cannot meet requirements or for large data flows between the database and application server.

Uploaded by

Fayazu Palagiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views73 pages

ABAP Managed Database Procedure

ABAP Managed Database Procedures (AMDP) allow developers to call stored procedures in the database from ABAP. AMDP procedures are implemented as methods in AMDP classes using database-specific languages like SQL Script. AMDP supports polymorphism and calling procedures between each other. It integrates with BAdIs and requires authorizations to manage database procedures. AMDP should only be used when Open SQL cannot meet requirements or for large data flows between the database and application server.

Uploaded by

Fayazu Palagiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 73

AMDP - ABAP Managed Database Procedures

ABAP Managed Database Procedures are a framework for managing and


calling stored procedures or database procedures in AS ABAP. An ABAP
Managed Database Procedure (or AMDP procedure) is a procedure written in
a database-specific language ( Native SQL, SQL Script, L, ...) implemented
in an AMDP method of an AMDP class.

 AMDP Classes

 AMDP Methods

 AMDP Polymorphism

 AMDP Calls

 AMDP BAdIs

 AMDP Database Connections

Notes

 The ABAP Development Tools (ADT) are better suited for the editing


of database procedures in AMDP methods than Class Builder in ABAP
Workbench. This means that AMDP classes can only be edited with the
ABAP Development Tools (ADT).

 The AS ABAP logged on to the database system using its work


processes requires the correct authorizations to manage database
procedures using AMDP. Missing authorizations can be detected using
transaction SICK.

 If the current database of an AS ABAP is an SAP HANA database and


ABAP programs access SQL Script procedures used to swap out code
from ABAP programs to the database, ABAP Managed Database
Procedures are the replacement technology for database procedure
proxies. Database procedure proxies are still recommended when using
a secondary database connection to access SQL Script procedures that
exist in a different SAP HANA database.
 Currently, AMDP only supports database procedures from the SAP
HANA database. In principle, however, AMDP is designed so that
stored procedures from other database systems can also be supported.

Programming Guideline

The programming guideline Use Open SQL is particularly relevant for


AMDP too. The use of AMDP is not recommended if the same task can be
achieved using Open SQL (or ABAP CDS). The example AMDP,
Comparison with Open SQL demonstrates how a database access not
programmed well in Open SQL can often be optimized using an improved
use of Open SQL, removing the need to use AMDP in these cases. AMDP
should be used only if it enables database-specific functions to be accessed
that do not exist in Open SQL (see the example for Currency Conversion) or
if large process flows or analyses that incur repeated transports of large
amounts of data between the database and the application server can be
swapped out.

AMDP Classes

An AMDP class is a global class in the Class Library that contains one or


more of the following tag interfaces:

 IF_AMDP_MARKER_HDB for the SAP HANA database

The names of the interfaces all start with IF_AMDP_MARKER and a suffix
indicates the database system for which the ABAP Managed Database
Procedures can be implemented in AMDP methods of the class.

An AMDP class can contain both regular methods and AMDP methods. It
can contain one or more AMDP methods for each database system specified
by a tag interface.

AMDP Methods

An AMDP is declared in an AMDP class like a regular static


method or instance method in any visibility section. An AMDP method
cannot be identified as an AMDP method in the declaration section of the
class.
The following restrictions apply, however, with respect to the parameter
interface:

 The typing of the parameters cannot be generic. Only elementary data


types and table types with a structured row type can be used. The row
type of a tabular type can only contain elementary data types as
components

 A parameter cannot be typed with a data type that references one of the
obsolete predefined data types DF16_SCL or DF34_SCL in ABAP
Dictionary.

 The parameters must be declared using VALUE for pass by value. Pass


by reference is not permitted.

 Return values cannot be declared using RETURNING.

 Only input parameters can be flagged as optional and every optional


parameter must have a replacement parameter declared
using DEFAULT. Only literals or constants can be specified as
replacement parameters.

 Parameter names:

o Parameter names cannot start with the characters "%_".

o The parameter name connection can only be used for an input


parameter of type DBCON_NAME, if the name of the database
connection can be passed to the input parameter.

o The parameter name client is reserved for future enhancements.

o The parameter name endmethod is not permitted.

 With RAISING the exception classed listed under AMDP - exception


classes can be specified to handle these classes for a call. Other
exceptions cannot be handled. No non-class-based exceptions can be
created using the addition EXCEPTIONS.
The following restrictions apply to method implementation:

 DDL statements are not permitted for creating, changing or deleting


database objects.

 Local temporary database objects cannot be accessed.

 Database commits and rollbacks cannot be executed. The


statements COMMIT and ROLLBACK in particular are not
permitted. LUWs should always be handled in the ABAP program, to
ensure data consistency between procedures.

 Write access to database tables, for which SAP buffering is activated, is


not permitted.

 AMDP methods do not have any implicit enhancement options.

An AMDP method is indicated as an AMDP method in the implementation


section of the class using the addition BY DATABASE PROCEDURE of
the statement METHOD. At the same time, the database system where the
method is used and the programming language of the database system used to
implement the method are also defined. Furthermore, the database objects of
the current database schema must be declared that are accessed in the AMDP.
If possible, the syntax check is performed on the basis of this information. An
AMDP method cannot be empty.

Further restrictions may apply depending on the programming language of


the database system and these are discussed in the corresponding sections:

 SQL Script for the SAP HANA Database

 L for the SAP HANA Database (used internally at SAP only)

Note

It is strongly recommended that only the character set 7-bit-ASCII is used


when implementing an AMDP method.

AMDP Polymorphism
Both interface methods and redefined methods inherited from superclasses
can become AMDP in the implementation, provided that the above
prerequisites are met and the class contains the appropriate tag interface. If a
subclass contains a tag interface, this also applies to its subclasses. If an
interface contains a tag interface, this also applies to classes that implement
this interface.

Constructors cannot be implemented as AMDP methods.

AMDP Calls

An AMDP method is called in an ABAP program in the same way as any


other ABAP Objects method. This requires, however, that the central
database of the current AS ABAP is managed by the database system for
which the AMDP method is implemented. If not, a runtime error is produced.

Before the method is called for the first time, the ABAP runtime environment
creates the procedure implemented in the AMDP method in the database
system of the central AS ABAP database or updates any existing database
procedure. Execution is performed in the database system and the interface
parameters are passed from the Native SQL interface to the database system
or applied by the database system itself.

Once a database procedure (managed using AMDP) has been created on the
database, this procedure can be called from other database procedures using
the database syntax (provided that this is permitted by the database). These
can be AMDP procedures (or database procedures) that are not managed by
AMDP. If an AMDP procedure calls another procedure, this procedure must
be specified after addition USING of the calling method.

Notes

 It is recommended that AMDP methods that are not to be called from


the AMDP methods of other classes are created as private methods of
an AMDP class and that they are called in a regular method (specified
by the central AS ABAP database. In database systems that do not
support AMDP, a regular method can be created using an alternative
implementation in Open SQL or Native SQL.
 For the calling program of an AMDP method from ABAP, it is usually
clear whether the method is an AMDP method and in which database
system it is executed. For this reason, the implementation of the method
must ensure that the interface parameters are returned in a format
expected by the calling program. This is particularly important for
ABAP types that do not have a corresponding type in the type system
of the database system. These types must either be forbidden in the
interface or an appropriate casting must take place in the procedure.

 Generally, the database procedures managed using AMDP are called


using AMDP methods only. If, in exceptional cases, these database
procedures are called from other database procedures not managed
using AMDP, all required database objects must be made available.
This can be done using the public methods of the documented
class CL_AMDP_RUNTIME_SERVICES.

AMDP BAdIs

In addition to the option of implementing BAdI methods of a normal BAdI as


AMDP methods and making these methods callable by using CALL BADI,
it is also possible to create special AMDP BAdIs. A AMDP BAdI is a BAdI,
which is labeled accordingly in BAdI Builder and has the following
properties:

 Definition and implementation

o An AMDP BAdI does not currently have any filters.

o Fallback BAdI implementation classes must be specified.

o Every BAdI method of an AMDP BAdI must be an AMDP method.

o Every AMDP method must be implemented for the same database


system.

 Call BAdI methods


o The AMDP methods of an AMDP BAdI can be called like
normal BAdl methods with GET BADI and CALL BADI.

o The AMDP procedures created on the database of an AMDP BAdI


can be called here (like all AMDP procedures) from other AMDP
methods in the same database system. These AMDP procedures are
specified using the name of a BAdI implementation class. The
usage must be declared after the addition USING of the calling
method.

In both cases, execution is performed in accordance with the rules of


the enhancement concept and the corresponding switch in Switch
Framework. If no enhancement is available, the mandatory fallback
implementation class is implemented.

Note

AMDP BAdIs are primarily used to execute calls of AMDP procedures from
AMDP procedures - in accordance with the rules of the switch-based
enhancement concept. Although switch-based calls of AMDP methods can be
made from ABAP programs without AMDP BAdIs, these calls can only be
made from AMDP procedures for AMDP methods of AMDP BAdIs. The
AMDP framework ensures that the relevant database procedure for the
current database switch setting is created on the database.

AMDP Database Connections

The implementation of an AMDP method is always executed on the SAP


standard database. By default, the call is made using the standard database
connection. The database connection to the SAP standard database can also
be specified explicitly (optional), which enables the use of service
connections.

To specify the database connection explicitly, an input parameter (with the


predefined name connection) can be declared for an AMDP method. This
parameter must be of type DBCON_NAME. When the AMDP method is
called, the following values can be passed to the parameter to specify the
database connection:
 When the initial value or the value "DEFAULT" is passed, the standard
database connection is used.

 When a name "R/3*name" is passed, which consists of the prefix


"R/3*" (uppercase characters) and a user-defined name "name" (case
sensitive), a service connection of this name is used in open SQL
(similar to when addition CONNECTION is used). The names
"R/3*AMDP_SYNC" and "R/3*AMDP_ADBC" are exceptions. They
are used by the AMDP framework itself.

All other names raise an exception.

Notes

 The parameter connection cannot be used to implement the AMDP


method.

 Service connections for the SAP standard database are useful for
performing operations in a database LUW, which does not depend on
the LUW standard connection.

 A maximum of 16 database connections can be opened at the same


time for a work process. This means a maximum of 15 service
connections is possible, in addition to the standard database connection.

Examples

See AMDP - Examples.

Continue
METHOD - BY DATABASE PROCEDURE
AMDP - Exception Classes
AMDP - Examples

AMDP - Exception Classes


The names of the exception classes of the exceptions that can be handled
when an AMDP method is called start with the prefix CX_AMDP. The
exceptions are all in the category CX_DYNAMIC_CHECK and must be
declared explicitly using RAISING in the definition of an AMDP method to
be handleable when this method is called. The meaning of the exception
classes is documented in their semantic attributes in the class library.

CX_ROOT 
  | 
  |--CX_DYNAMIC_CHECK 
       | 
       |--CX_AMDP_ERROR 
           | 
           |--CX_AMDP_VERSION_ERROR 
           |    | 
           |    |--CX_AMDP_VERSION_MISMATCH 
           | 
           |--CX_AMDP_CREATION_ERROR 
           |    | 
           |    |--CX_AMDP_DBPROC_CREATE_FAILED 
           |    | 
           |    |--CX_AMDP_NATIVE_DBCALL_FAILED 
           |    | 
           |    |--CX_AMDP_WRONG_DBSYS 
           | 
           |--CX_AMDP_EXECUTION_ERROR 
           |    | 
           |    |--CX_AMDP_EXECUTION_FAILED 
           |    | 
           |    |--CX_AMDP_IMPORT_TABLE_ERROR 
           |    | 
           |    |--CX_AMDP_RESULT_TABLE_ERROR 
           | 
           |--CX_AMDP_CONNECTION_ERROR 
                | 
                |--CX_AMDP_NO_CONNECTION 
                | 
                |--CX_AMDP_NO_CONNECTION_FOR_CALL 
                | 
                |--CX_AMDP_WRONG_CONNECTION 

METHOD - BY DATABASE PROCEDURE 

Syntax

METHOD meth BY DATABASE PROCEDURE 


            FOR db 
            LANGUAGE db_lang 
            [OPTIONS db_options] 
            [USING   db_entities]. 
  ... 
ENDMETHOD. 

Extras:

1. ... FOR db 

2. ... LANGUAGE db_lang 

3. ... OPTIONS db_options 

4. ... USING db_entities 

Effect

Implements an AMDP method in an AMDP class. The addition BY


DATABASE PROCEDURE can be specified in the implementation
section of a global class that contains an appropriate tag interface
IF_AMDP_MARKER_db. The parameter interface of the method declared in
the declaration section must meet the prerequisites for AMDP methods.

The addition FOR db defines the database system where the method is used.
The addition LANGUAGE defines the database-specific language in which
the method is implemented. OPTIONS can be used to declare database-
specific options and USING can be used to declare the database objects
managed by ABAP for use in the AMDP method.

Note

If the current AS ABAP does not have sufficient authorizations to manage


database procedures in its database system, syntax errors can occur in AMDP
methods. Missing authorizations can be detected using transaction SICK.

Examples

See AMDP - Examples. 

Addition 1

... FOR db 

Effect

Defines the database system for which the AMDP method is used. The
AMDP class must contain the appropriate tag interface. The following table
shows the syntax that can be specified for db, the database system it
identifies, and the required tag interface.

db Database System Tag Interface


HD SAP HANA IF_AMDP_MARKER_
B database HDB

The AMDP method can only be called in an AS ABAP whose central


database is managed by the specified database system; otherwise a runtime
error is produced.

Notes
 An AMDP class can contain multiple tag interfaces for different
database systems for implementing AMDP methods in various
languages. 

 To define an AMDP class as global, it is recommended that a regular


method is offered that calls AMDP methods as specified by the
database system of the central database of the current AS ABAP. In
database systems that do not support AMDP, a regular method can be
created using an alternative implementation in Open SQL or Native
SQL. 

Addition 2

... LANGUAGE db_lang 

Effect

Defines the database-specific language in which the AMDP is implemented.


The following table shows the syntax for db_lang used to specify the
language (depending on the specified database system db), the meaning, and
a link to further information.

Languag
db db_lang More Information
e
HD SQLSCRI SQLScri
SQLScript for the SAP HANA database
B PT pt
HD L for the SAP HANA database (used internally at
LLANG L
B SAP only)

If possible, the ABAP syntax check examines the implementation of the


method for the specified language. This is possible only if the check takes
place on an AS ABAP whose central database is managed by the specified
database system. The check is performed with respect to the currently
installed version of the database. No checks take place on an AS ABAP with
a different database system. 

Addition 3

... OPTIONS db_options 

Effect

Specifies database-specific options for the AMDP procedure. The following


tables shows the syntax for option as specified by the database system in
question, db and the associated meaning.

db option Meaning
HD READ- Only reads are permitted on the database tables in the
B ONLY database procedure.
For internal use at SAP only: If database objects in the
SUPPRESS AMDP procedure are accessed, which do not exist at the
HD
SYNTAX time of the syntax check, then only syntax warnings are
B
ERRORS raised and not syntax errors. If a required object is missing
at runtime, a catchable exception is raised.

The syntax check examines the specified options at runtime.

Note

The addition OPTIONS READ-ONLY must be specified in the


implementation of L procedures. 

Addition 4

... USING db_entities 

Effect
Declares the database objects managed by ABAP accessed in the AMDP
procedure. The following must be specified after USING:

 All database tables and views from ABAP Dictionary 

 All procedures managed by AMDP 

These tables, views, and procedures are from the current database schema
used in the procedure. These database objects are specified after USING and,
when used in the procedure, must be specified directly and without
specifying the database schema. An AMDP procedure is specified using the
syntax class=>meth. Here, class is the name of its AMDP class and meth is
the name of the AMDP method.

Each database object specified after USING must also be used in the


procedure. Any database objects other than those above managed by ABAP
cannot be specified after USING and the procedure cannot be used to access
database objects not specified after USING, with the following exceptions:

 Database objects in the current database schema if they are not in the
namespace of the namespace prefix /1BCAMDP/. If used in the
procedure, they must be specified directly and without specifying the
database schema. 

 Database objects from a different database schema using their full


qualified names. 

With the exception of the namespace /1BCAMDP/, database objects from the


current database schema can be accessed in a database procedure managed by
AMDP only if they are managed by ABAP and specified after USING.

Notes

 Explicit declarations of database objects used in the procedure are


required for various static checks and enable, for example, the visibility
of an AMDP method to be passed to the database level. 

 An object specified after USING must be visible here. An AMDP


method from a different class can be specified only if it can be used by
the current class and, accordingly, can only be called in the database
procedure. This applies the visibility concept in ABAP Objects to the
database system, even if it does not have a similar concept. 

 When an AMDP method class=>meth is specified after USING, the


ABAP runtime environment identifies this as a use of the
class class and itsstatic constructor is executed before the first call of
the AMDP method. This means that, if an AMDP procedure calls other
AMDP procedures, the corresponding static constructor is executed. 

 AMDP methods in the same class must also be specified after USING,


if used in the procedure. 

 The namespace /1BCAMDP/ is intended for temporary database tables,


views, or database procedures of the current database schema accessed
in AMDP procedures. 

 The addition USING cannot be specified in the implementation of L


procedures. 

Continue
AMDP - SQL Script for the SAP HANA Database
AMDP - L for the SAP HANA Database

AMDP - SQL Script for the SAP HANA Database

SQL Script is a script language, documented in the SAP HANA SQLScript


Reference, and used to program SQLScript procedures in the SAP HANA
database. It is the implementation language of an AMDP method that
specifies the addition FOR HDB LANGUAGE SQLSCRIPT when the
statement METHOD is used.

 AMDP Procedures in the SAP HANA Database System

 Syntax
 Parameter Interface

o Mapping with Reference to Types Defined in ABAP

o Mapping with Reference to Types Defined in ABAP Dictionary

 Calls

Notes

 Before AS ABAP is able to manage SQL Script procedures on the SAP


HANA database, it requires the following authorizations as a user of
the database system:

o Privilege Execute on the object GET_PROCEDURE_OBJECTS of


the schema SYS

o Privilege Execute on the
object TRUNCATE_PROCEDURE_OBJECTS of the schema SYS

Missing authorizations can be detected using transaction SICK.

 When used in AMDP, SQL Script replaces calls of corresponding


database procedures using database procedure proxies and the
statement CALL DATABASE PROCEDURE. The required
procedure can either be implemented in full in an AMDP method or an
existing database procedure can be called from an AMDP method. In
this case, the AMDP method replaces the database procedure proxy.
Database procedure proxies are still recommended for scenarios in
which secondary database connections are used to access existing
procedures SAP HANA database alongside the current central AS
ABAP database.

AMDP Procedures in the SAP HANA Database System

The SQL Script implementation of an AMDP method with the name meth in


an AMDP class class is saved by the ABAP runtime environment as an
SQLScript procedure under the name CLASS=>METH in the database
schema SAPsid of the SAP HANA database. Note that these names are case-
sensitive when used in the database system. AS ABAP is responsible for all
transports, administration, and access control. SQL Script procedures
managed using AMDP can be accessed as follows:

 Calls from ABAP

Calls of the associated AMDP method using the regular method


call meth( ... ) in ABAP programs.

 Calls from other AMDP procedures

An AMDP procedure implemented using SQL Script in the same class


or a different class can call another AMDP procedure with the
usualSQLScript syntax:
CALL "CLASS=>METH"( f1 => a1, f2 => a2, ... );
The called method must be specified after the addition USING of the
calling method. The usual visibility rules from ABAP Objects apply. A
database procedure implemented in a class cannot call a method
implemented in a private AMDP method of a different class, unless a
friendship exists between the classes.

 Calls from regular database procedures

An SQL Script procedure created in the regular way in SAP HANA


Studio or using Native SQL can call an SQL Script procedure
implemented in an AMDP method, if this procedure exists in the
database system. This is not recommended, however, since the AMDP
procedures are managed in AS ABAP and consistency can only be
guaranteed for other AMDP procedures from the same AS ABAP.

 Access in SAP HANA Studio

Once the ABAP runtime environment has created an SQL Script


procedure managed by AMDP in the database system, it is visible
in SAP HANA Studio and can even be edited. This is not
recommended, however, since changes like this are ignored by the
implementation in the AMDP method and can be overwritten by the
ABAP runtime environment at any time.

Syntax

The syntax of a SQL Script procedure written in SQL Script is exactly as


described under SAP Hana SQLScript Reference, with one exception: The
character * at the start of a line indicates a comment line, as in ABAP. When
the procedure is saved in the database system, the asterisk, *, is transformed
to the usual double hyphens, --.

All SQL Script operators CE_... that support views can be used. SQL Script
operators, which do not support any views (for
example, CE_COLUMN_TABLEonly works with the technical
property Column Store) cannot be used.

Notes

 Support for ABAP comments introduced using * enables the use of


change entries inserted by the Modification Assistant tool in ABAP
programs. This property should not be used when creating AMDP
methods in the regular way.

 The syntax is checked only on an AS ABAP whose central database is


a SAP HANA database. Here, a temporary database procedure is
created that checks the syntax.

Parameter Interface

The parameter interface of an SQL Script procedure uses input parameters


declared using IN, output parameters declared using OUT, and input/output
parameters declared using INOUT. All parameters can be scalar and tabular.
When used in the procedure, certain operand positions expect or allow the
name of an input parameter to be prefixed with a colon (:).

The parameter interface of an AMDP method that implements an SQL Script


procedure is transformed accordingly:
 An input parameter of the method defined
using IMPORTING becomes an input parameter of the procedure
declared using IN.

 An output parameter of the method defined


using EXPORTING becomes an output parameter of the procedure
declared using OUT.

 If CHANGING is used to define a

o scalar input/output parameter of the method, the parameter becomes


an input/output parameter of the database procedure declared
using INOUT and if it is used to define a

o tabular input/output parameter of the method, it is transformed to a


pair of input and output parameters declared using IN and OUT on
the database, since SQL Script does not support INOUT parameters.
The OUT parameter has the name of the CHANGING parameter
and is used instead of this parameter in the AMDP procedure.
The IN parameter is created with an internal name comprising the
name of the CHANGING parameter and a postfix __IN__.
The IN parameter is assigned to the OUT parameter before the start
of the actual AMDP procedure. This transformation and the
associated call are generally transparent for all ABAP developers. It
becomes visible only when the database procedure is displayed, for
example, in SAP HANA Studio or called from another database
procedure (see the example].

Elementary and tabular method parameters become scalar and tabular


parameters of the database procedure respectively. As well as the restrictions
for the parameter interface of an AMDP method described under AMDP, the
following restrictions apply in the implementation with SQL Script:

 An input/output parameter declared using CHANGING cannot have


the type string or xstring. An exception this rule are parameters that
are typed with reference to the predefined type SSTRING ABAP
Dictionary.
 The length of a parameter typed with the type c or n is restricted to a
maximum of 5000 characters.

 Parameters of the types f, decfloat16, decfloat34, string,


and xtring cannot be assigned a replacement value using DEFAULT,
which means they cannot be optional parameters.

As shown in the following tables, the elementary ABAP types are mapped to
the appropriate types in SQL Script. The tables distinguish parameters that
reference a type defined in ABAP from those that reference a predefined type
in ABAP Dictionary. No AMDP procedures can be created for SQL Script
types that are not listed here.

Note

In SQL Script, tabular parameters are handled as temporary database tables.


An internal table passed to SQL Script can be accessed there just like a
database table.

Mapping with Reference to Types Defined in ABAP

Numeric Types

ABAP Type HANA Type Note


SMALLINT is a two
b SMALLINT
byte integer with sign
SMALLINT is a two
s SMALLINT
byte integer with sign
i INTEGER -
p, DECIMAL,
length leng with dec decimal length 2len-1 with de -
places cdecimal places
decfloat16 VARBINARY, length Cannot be interpreted
8 as a number in the
database and no
calculations can be
performed
Cannot be interpreted
as a number in the
VARBINARY, length
decfloat34 database and no
16
calculations can be
performed
f DOUBLE -

Character-Like Types

ABAP
HANA Type Note
Type
c, NVARCHAR
-
length len , length len
string NCLOB Not permitted for input/output parameters
A suitable casting to a numeric database type must
n, NVARCHAR be performed before calculations can be made in
length len , length len the database. Numeric results must be transformed
back to the ABAP format.
A casting to the DATE type of the database must
NVARCHAR
d be performed before date calculations can be made.
, length 8
SQL Script offers the predefined function to_dats.
A casting to the TIME type of the database must be
NVARCHAR
t performed before time calculations can be made.
, length 6
SQL Script offers the predefined function to_tims.

Byte-Like Types
ABAP
HANA Type Note
Type
x, VARBINARY,
-
length len length len
xstring BLOB Not permitted for input/output parameters

Mapping with Reference to Types Defined in ABAP Dictionary

The predefined types in ABAP Dictionary are mapped like the associated


ABAP types, with the following exceptions.

Dictionary Type HANA Type Note


DF16_DEC, DECIMAL, length 2len- Value range on
length leng with dec decimal 1 with dec decimal the database is
places places less than in ABAP
DF34_DEC, DECIMAL, length 2len- Value range on
length leng with dec decimal 1 with dec decimal the database is
places places less than in ABAP
Obsolete, not
DF16_SCL -
supported
Obsolete, not
DF34_SCL -
supported
NVARCHAR,
SSTRING, length len -
length len

Calls

As described above, SQL Script procedures managed in the SAP HANA


database using AMDP can be called from ABAP programs and from other
AMDP procedures. How they can be used is determined by the visibility of
the AMDP methods. Recursive and cyclical calls are forbidden in the HANA
database system, which means that

 an SQL Script procedure cannot call itself

 an AMDP procedure that implements an SQL Script procedure cannot


call any database procedures that themselves call the AMDP procedure.

Furthermore, an AMDP procedure can call regular SQL Script procedures


created in SAP HANA Studio or using Native SQL. These procedures cannot
and must not be specified after the addition USING of the calling AMDP.

Examples

See AMDP - Examples.

AMDP, Implementation of a SQLScript Procedure

This example demonstrates how an SQLScript procedure is implemented


using AMDP.

Source Code

REPORT demo_amdp.

CLASS demo DEFINITION.


  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.


  METHOD main.
    DATA incprice     TYPE sflight-price.
    DATA price_before TYPE sflight-price.
    DATA price_after  TYPE sflight-price.
    IF cl_db_sys=>is_in_memory_db = abap_false.
      cl_demo_output=>display(
        `Example can be executed on SAP HANA Database only` ).
      LEAVE PROGRAM.
    ENDIF.

    cl_demo_input=>request( CHANGING field = incprice ).


    IF incprice IS INITIAL.
      RETURN.
    ENDIF.

    SELECT SINGLE price FROM sflight INTO price_before.

    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.

    SELECT SINGLE price FROM sflight INTO price_after.


    IF price_after - price_before = incprice.
      cl_demo_output=>display( `Price increased succesfully` ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

A simple SQLScript procedure is implemented in the AMDP method


INCREASE_PRICE of the AMDP class CL_DEMO_AMDP:

METHOD increase_price BY DATABASE PROCEDURE FOR


HDB 
                      LANGUAGE SQLSCRIPT 
                      USING sflight. 
  update sflight set price = price + :inc 
                 where mandt = :clnt; 
ENDMETHOD.

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 examples for ADBC and for CALL DATABASE
PROCEDURE. AMDP replaces these technologies when calling database
procedures for an SAP HANA database that is the central database of an AS
ABAP.

Note

This is a syntax example. The same function can be provided with the same
efficiency in Open SQL. AMDP is not needed in simple cases like this.

AMDP, Calling an AMDP Procedure from SQLScript

This example demonstrates how an AMDP procedure is called from an


AMDP procedure.

Source Code

REPORT demo_amdp_call_amdp_procedure.

CLASS demo DEFINITION.


  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.


  METHOD main.
    DATA incprice TYPE sflight-price.
    DATA price_before TYPE sflight-price.
    DATA price_after TYPE sflight-price.

    IF cl_db_sys=>is_in_memory_db = abap_false.


      cl_demo_output=>display(
        `Example can be executed on SAP HANA Database only` ).
      LEAVE PROGRAM.
    ENDIF.

    cl_demo_input=>request( CHANGING field = incprice ).


    IF incprice IS INITIAL.
      RETURN.
    ENDIF.

    SELECT SINGLE price FROM sflight INTO @price_before.

    TRY.
        cl_demo_amdp_call_amdp=>increase_price( clnt     = sy-mandt
                                                incprice = incprice ).
      CATCH cx_amdp_error INTO DATA(amdp_error).
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    SELECT SINGLE price FROM sflight INTO @price_after.


    IF price_after - price_before = incprice.
      cl_demo_output=>display( `Price increased succesfully` ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description
The SQLScript procedure of the AMDP method INCREASE_PRICE of the
AMDP class CL_DEMO_AMDP_CALL_AMDP calls a further AMDP
procedure implemented in the private AMDP method
INCREASE_PRICE_AMDP in the same class:

METHOD increase_price BY DATABASE PROCEDURE 


                      FOR HDB LANGUAGE SQLSCRIPT 
                      USING
cl_demo_amdp_call_amdp=>increase_price_amdp. 
  call
"CL_DEMO_AMDP_CALL_AMDP=>INCREASE_PRICE_AMD
P"( 
    CLNT => :CLNT, INCPRICE => :INCPRICE ); 
ENDMETHOD.
METHOD increase_price_amdp BY DATABASE PROCEDURE 
                           FOR HDB LANGUAGE SQLSCRIPT 
                           USING sflight. 
  update sflight set price = price + incprice 
               where mandt = clnt; 
ENDMETHOD.

The called database procedure must be specified after the addition USING of


the statement METHOD. Compare the example for calling a further database
procedure not managed using AMDP.

AMDP, Calling an SQLScript Procedure from AMDP

This example demonstrates how a database procedure is called from an


AMDP procedure.

Source Code
REPORT demo_amdp_call_db_procedure.

CLASS demo DEFINITION.


  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.


  METHOD main.
    DATA incprice TYPE sflight-price.
    DATA price_before TYPE sflight-price.
    DATA price_after TYPE sflight-price.

    IF cl_db_sys=>is_in_memory_db = abap_false.


      cl_demo_output=>display(
        `Example can be executed on SAP HANA Database only` ).
      LEAVE PROGRAM.
    ENDIF.

    cl_demo_input=>request( CHANGING field = incprice ).


    IF incprice IS INITIAL.
      RETURN.
    ENDIF.

    SELECT SINGLE price FROM sflight INTO @price_before.

    TRY.
        cl_demo_amdp_call=>increase_price( incprice ).
      CATCH cx_amdp_error INTO DATA(amdp_error).
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    SELECT SINGLE price FROM sflight INTO @price_after.


    IF price_after - price_before = incprice.
      cl_demo_output=>display( `Price increased succesfully` ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

The SQLScript procedure of the AMDP method INCREASE_PRICE in the


AMDP class CL_DEMO_AMDP_CALL calls a database procedure (not
managed by AMDP) of the current database schema in the
namespace /1BCAMDP/:

METHOD increase_price BY DATABASE PROCEDURE FOR


HDB LANGUAGE SQLSCRIPT. 
  call "/1BCAMDP/ABAP_DOCU_DEMO_INCPRICE"( INC
=> :INCPRICE ); 
ENDMETHOD.

The called database procedure is created dynamically in the static constructor


of the AMDP class CL_DEMO_AMDP_CALL using ADBC. It is in the
same database schema, but due to the namespace /1BCAMDP/ it cannot and
must not be used after the addition USING of the METHOD statement.
Compare this example when calling a further AMDP procedure.

AMDP, AMDP Methods in Interfaces and Superclasses

This example demonstrates how AMDP methods are implemented from


interfaces and superclasses.

Source Code

REPORT demo_amdp_polymorphism.

CLASS demo DEFINITION.


  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.


  METHOD main.
    DATA: iref TYPE REF TO if_demo_amdp_interface,
          cref TYPE REF TO cl_demo_amdp_superclass.

    CASE cl_db_sys=>is_in_memory_db.
      WHEN abap_true.
        cref = NEW cl_demo_amdp_subclass_hdb( ).
        iref = NEW cl_demo_amdp_implement_hdb( ).
      WHEN abap_false.
        cref = NEW cl_demo_amdp_subclass_open( ).
        iref = NEW cl_demo_amdp_implement_open( ).
    ENDCASE.

    TRY.
        iref->get_scarr( EXPORTING clnt     = sy-mandt
                         IMPORTING carriers = DATA(result1) ).
        cref->get_scarr( EXPORTING clnt     = sy-mandt
                         IMPORTING carriers = DATA(result2) ).
      CATCH cx_amdp_error INTO DATA(amdp_error).
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    ASSERT result1 = result2.

    cl_demo_output=>display( name = 'Result'


                             data = result1 ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).
Description

An interface IF_DEMO_AMDP_INTERFACE and an abstract
superclass CL_DEMO_AMDP_SUPERCLASS implement the tag interface
IF_AMDP_MARKER_HDB and contain the method GET_CARRIER,
which meets the prerequisites of an AMDP method.

The interface method is implemented once as an AMDP method and once as


a regular method in the
classes CL_DEMO_AMDP_IMPLEMENT_HDB and CL_DEMO_AMDP_I
MPLEMENT_OPEN. In the same way, the method of the abstract superclass
is redefined in the fixed
subclasses CL_DEMO_AMDP_SUBCLASS_HDB and CL_DEMO_AMDP
_SUBCLASS_OPEN.

This program demonstrates how objects are created using either the AMDP
method or the regular method and called polymorphically, as specified by the
current database.

AMDP, SQL Script with Tabular CHANGING Parameter

This example demonstrates how an SQLScript procedure is implemented


using AMDP with a tabular CHANGING parameter.

Source Code

REPORT demo_amdp_changing.

CLASS demo DEFINITION.


  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.


  METHOD main.
    IF cl_db_sys=>is_in_memory_db = abap_false.
      cl_demo_output=>display(
        `Example can be executed on SAP HANA Database only` ).
      LEAVE PROGRAM.
    ENDIF.

    DATA lower TYPE scarr-carrid VALUE 'AA'.


    DATA upper TYPE scarr-carrid VALUE 'BA'.
    DATA call_flag TYPE abap_bool.
    cl_demo_input=>new(
       )->add_field( CHANGING field = lower
       )->add_field( CHANGING field = upper
       )->add_line(
       )->add_field( EXPORTING text = 'Indirect call'
                               as_checkbox = abap_true
                     CHANGING  field = call_flag
       )->request( ).

    DATA carriers TYPE cl_demo_amdp_changing=>t_carriers.


    SELECT mandt, carrid
           FROM scarr
           WHERE carrid BETWEEN @lower AND @upper
           ORDER BY mandt, carrid
           INTO CORRESPONDING FIELDS OF TABLE @carriers.
    DATA(out) = cl_demo_output=>new( )->write( carriers ).

    TRY.
        IF call_flag IS INITIAL.
          NEW cl_demo_amdp_changing(
            )->get_carriers( CHANGING carriers = carriers ).
        ELSE.
          NEW cl_demo_amdp_changing(
            )->call_get_carriers( CHANGING carriers = carriers ).
        ENDIF.
      CATCH cx_amdp_error INTO DATA(amdp_error).
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    out->display( carriers ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

The following SQLScript procedure is implemented in the AMDP method


GET_CARRIERS of the AMDP class CL_DEMO_AMDP_CHANGING:

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.

The tabular CHANGING parameter carriers can be used in this way in the


procedure in reader and writer positions. Internally, the database procedure
uses an identically named output parameter to which the initial value of
the CHANGING parameter is assigned (using the
invisible IN parameter carrriers__in__) when the procedure is first executed
(see SQL Script for the SAP HANA Database).

A further method, CALL_GET_CARRIERS, demonstrates how the SQL


Script procedure implemented in GET_CARRIERS is called from a different
SQL Script procedure.

METHOD call_get_carriers BY DATABASE PROCEDURE FOR


HDB 
                            LANGUAGE SQLSCRIPT 
                         USING cl_demo_amdp_changing=>get_carriers. 
  call "CL_DEMO_AMDP_CHANGING=>GET_CARRIERS"( 
    CARRIERS__IN__  => :CARRIERS, 
    CARRIERS        => :CARRIERS ); 
ENDMETHOD.

In this case, the implicit IN parameter carrriers__in__ must be filled explicit


with the modified actual parameter.

Note

This is a syntax example. The same function can be provided with the same
efficiency in Open SQL. AMDP is not needed in simple cases like this.

AMDP, AMDP Method with Specified Database Connection

The example demonstrates an AMDP method with the input


parameter connection.

Source Code

REPORT demo_amdp_connection.

CLASS demo DEFINITION.


  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.


  METHOD main.
    IF cl_db_sys=>is_in_memory_db = abap_false.
      cl_demo_output=>display(
        `Example can be executed on SAP HANA Database only` ).
      LEAVE PROGRAM.
    ENDIF.

    DATA connection TYPE dbcon_name VALUE 'R/3*my_conn'.


    cl_demo_input=>request( CHANGING field = connection ).
    TRY.
        NEW cl_demo_amdp_connection(
              )->get_scarr( EXPORTING
                              connection = connection
                              clnt       = sy-mandt
                            IMPORTING
                              carriers = DATA(result) ).
      CATCH cx_amdp_error INTO DATA(amdp_error).
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    cl_demo_output=>display( result ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

A simple SQLScript procedure is implemented in the AMDP method


GET_SCARR of the AMDP class CL_DEMO_AMDP:

METHOD get_scarr BY DATABASE PROCEDURE FOR HDB 


                      LANGUAGE SQLSCRIPT 
                      USING scarr. 
  carriers  = select * 
                     from scarr 
                     WHERE mandt  = clnt; 
ENDMETHOD.

The method also has an optional input parameter with the predefined
name connection. This is used to specify whether the standard database
connection or a service connection is used. If a permitted name such as
"DEFAULT" or "R/3*my_conn" is passed, the method is executed using
either the standard connection or a service connection. Invalid names raise an
exception and an exception is text is displayed.

AMDP, Filling a Mesh with SQLScript

This example demonstrates how a mesh is filled with a database procedure.

Source Code

REPORT demo_amdp_mesh.

CLASS demo DEFINITION.


  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    TYPES:
      BEGIN OF output_line,
        position TYPE string,
        product  TYPE string,
      END OF output_line,
      output TYPE STANDARD TABLE OF output_line WITH
EMPTY KEY.
ENDCLASS.

CLASS demo IMPLEMENTATION.


  METHOD main.
    DATA order_ids TYPE RANGE OF snwd_so-so_id.
    DATA(rows) = 1.
    cl_demo_input=>request( EXPORTING text = `Orders`
                            CHANGING field = rows ).
    SELECT 'I' AS sign, 'EQ' AS option, so_id AS low
           FROM snwd_so
           ORDER BY low
           INTO CORRESPONDING FIELDS OF TABLE @order_ids
           UP TO @rows ROWS. "#EC CI_NOWHERE

    DATA(out) = cl_demo_output=>new( ).
    TRY.
        DATA(order) = NEW cl_demo_amdp_mesh( )-
>select( order_ids ).
      CATCH cx_amdp_error INTO DATA(amdp_error).
        out->display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    LOOP AT order-orders ASSIGNING FIELD-


SYMBOL(<order>).
      out->begin_section( |Order #{ <order>-so_id }| ).
      ASSIGN order-orders\to_buyers[ <order> ]
        TO FIELD-SYMBOL(<buyer>).
      out->begin_section(
        |Buyer: { <buyer>-company_name } | &&
        |in { order-bupas\to_addresses[ <buyer> ]-city } | ).
      out->write(
        name  = 'Sales Order Items'
        data = VALUE output(
          FOR <item> IN order-orders\to_items[ <order> ]
          ( position = <item>-so_item_pos
            product = order-items\to_products[ <item> ]-product_id
          ) ) ).
      out->end_section( )->end_section( ).
    ENDLOOP.
    out->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

The AMDP class CL_DEMO_AMDP_MESH has a private AMDP method


SELECT_HDB called by the public method SELECT if the current central
database is the SAP HANA database. For all other databases, the private
method SELECT_OPEN_SQL is called. Both methods return the same
results, verified by a unit test in the class.

The methods fill the nodes of a mesh for sales orders whose type is declared
in the AMDP class. The number of sales orders is restricted using a ranges
table. In the method SELECT_OPEN_SQL, the Open SQL addition FOR
ALL ENTRIES is used, but in SELECT_OPEN_SQL the intermediate
results can be evaluated in subqueries.

SQLScript implementation of the AMDP method

METHOD select_hdb  BY DATABASE PROCEDURE 


                   FOR HDB LANGUAGE SQLSCRIPT 
                   OPTIONS READ-ONLY 
                   USING snwd_so snwd_so_i snwd_pd snwd_bpa
snwd_ad. 
  orders = select * from snwd_so 
    where client = :clnt and so_id in ( 
      select low from :order_ids ); 

  bupas = select * from snwd_bpa 


    where client = :clnt and node_key in ( 
      select DISTINCT buyer_guid from :orders ); 

  adresses = select * from snwd_ad 


    where client = :clnt and node_key in ( 
      select address_guid from :bupas ); 

  items = select * from snwd_so_i 


    where parent_key in ( select node_key from :orders ); 

  products = select * from snwd_pd as product 


    where client = :clnt and node_key in ( 
      select distinct product_guid from :items ); 
ENDMETHOD.
ABAP implementation of the regular method

METHOD select_open_sql. 
  SELECT * 
         FROM snwd_so 
         INTO TABLE @orders 
         WHERE so_id IN @order_ids. 

  SELECT * 
         FROM snwd_so_i 
         INTO TABLE @items 
         FOR ALL ENTRIES IN @orders 
         WHERE parent_key = @orders-node_key. 

  SELECT * 
        FROM snwd_pd 
        INTO TABLE @products 
        FOR ALL ENTRIES IN @items 
        WHERE node_key = @items-product_guid. 

  SELECT * 
        FROM snwd_bpa 
        INTO TABLE bupas 
        FOR ALL ENTRIES IN orders 
        WHERE node_key = orders-buyer_guid 
        ##SELECT_FAE_WITH_LOB[WEB_ADDRESS]. 

  SELECT * 
         FROM snwd_ad INTO TABLE @adresses 
         FOR ALL ENTRIES IN @bupas 
         WHERE node_key = @bupas-address_guid. 
ENDMETHOD.

Note
This is a syntax example. The same function can be provided with the same
efficiency in Open SQL. AMDP is not needed in simple cases like this.

AMDP, Implementation of an L Procedure

This example demonstrates the implementation of a procedure in the


programming language L using AMDP.

Source Code

REPORT demo_amdp_l_language.

CLASS demo DEFINITION.


  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.


  METHOD main.

    IF cl_db_sys=>is_in_memory_db = abap_false.


      cl_demo_output=>display(
        `Example can be executed on SAP HANA Database only` ).
      LEAVE PROGRAM.
    ENDIF.

    DATA(text) = `World`.
    cl_demo_input=>request( CHANGING field = text ).
    IF text IS INITIAL.
      RETURN.
    ENDIF.

    TRY.
        NEW cl_demo_amdp_l_hello_world(
              )->hello_world( EXPORTING text  = text
                              IMPORTING texts = DATA(texts) ).
      CATCH cx_amdp_error INTO DATA(amdp_error).
        cl_demo_output=>display( amdp_error->get_text( ) ).
        RETURN.
    ENDTRY.

    cl_demo_output=>display( texts ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

The following L procedure is implemented in the AMDP method


HELLO_WORLD of the AMDP
class CL_DEMO_AMDP_L_HELLO_WORLD:

METHOD hello_world BY DATABASE PROCEDURE 


                   FOR HDB LANGUAGE LLANG 
                   OPTIONS READ-ONLY. 
* Hello World in L 
  typedef Table <String "TEXT"> TT_TABLE;  /* Type definition 
                                              repeated  */ 
  //Main entry point 
  export Void main(String text, TT_TABLE & texts) 
    { String hello = String("Hello "); 
      texts."TEXT"[0z] = hello.append( text ).append( "!" ); } 
ENDMETHOD.

The tabular output parameter is created from a predefined text and an


interactive input made in the ABAP program.

Note

The programming language L can only be used internally and with


restrictions at SAP. For general programming with the SAP HANA
database, SQL or the script language SQLScript must be used. The use of L
by customers and partners is not supported.
L procedure

A database procedure in the SAP HANA database programmed in the


language L, used internally at SAP. SQLScript procedures can be created
interactively in SAP HANA Studio. In addition, ABAP Managed Database
Procedures (AMDP) enable L procedures to be implemented in AMDP
methods of AMDP classes.

ABAP Managed Database Procedure (AMDP) in HANA

Definition of ABAP Managed Database Procedure (AMDP)

ABAP Managed Database Procedure (AMDP) is a class-based framework for


managing and calling database procedure in ABAP.

Along the lines of other ABAP for HANA tools, the editing of ABAP
Managed Database Procedures is only supported in the Eclipse-based
development environment (ABAP Development Tools) and not in SAP GUI
(Class Builder). In the SAP GUI mode, the ABAP Workbench will reject the
editing support, whereas viewing of code within the AMDP methods is still
possible.

Bottom-Up Approach with Database Procedure Proxies


As we know the currently, the optimized way for using HANA procedures
in ABAP is provide by Database Procedure Proxies which was introduced
with Application ABAP 7.4 with service package 2.

In this Bottom-up approach, the database has first to be developed in HANA


server and then expose the Database Procedure Proxy in ABAP server. To
ensure that dependent ABAP and HANA content is exported together
in HANA Transport Container and HANA Delivery Unit are required.

This Bottom-UP approach involves the problem of having a different


independent life cycle in TMS (Transport Management System) for the
HANA and ABAP content.

ABAP Managed Database Procedure (AMDP) (Top-Down Approach)


The solution for the problem faced in Bottom-Up approach is provided by
ABAP Managed Database Procedure (AMDP).

This Top-Down approach enables developers to create a managed whole life-


cycle of HANA procedure in ABAP Development Environment.The AMDP
is implemented as a method of a global class which is marked with specific
interfaces called as AMDP class.

In corresponding to AMDP class, the HANA based SQL class is created at


the first call of the method.

Advantage of AMDP Process

 The main advantage of this method that only the AMDP class has to be
transported wit ABAP transport mechanism.
 No HANA delivery or HANA transport system is required in this
process.
 Developers only need ABAP development tools for building and
managing the CDS view. There is no need for additional HANA
development tools.

Example of AMDP Class Definition

CLASS CL_AMBP_EXAMPLE DEFINITION.

PUBLIC SECTION.
INTERFACES IF_AMDP_MARKER_HDB. //Marker Interface for
HANA DB//

METHODS process //Only ABAP code is possible//


IMPORTING it_param TYPE type1
EXPORTING et_param TYPE type2.

METHODS execute //SQLScript or ABAP code both are possible//


IMPORTING VALUE(it_param) TYPE type1
EXPORTING VALUE(et_param) TYPE type2. //specific parameter
interface required//
CHANGING VALUE(ch_param) TYPE type3

ENDCLASS.

AMDP Class Implementation

CLASS CL_AMDP_EXAMPLE IMPLEMENTATION

METHODS process
// Write ABAP source code here//
...
ENDMETHOD

METHOD execute BY DATABASE PROCEDURE //AMDP method


marker//
FOR HDB //Database platform//
LANGUAGE SQLScript //Database language//
[OPTIONS READ-ONLY] //Database-specific options//
USING name1 name2 etc... //List of used DDIC entities and AMDPs//
//Write here the SQLScript coding// 
select * from dummy;
...
ENDMETHOD.
ENDCLASS.

Features of ABAP Managed Database Procedure (AMDP)

 Static check code and Syntax colouring are provided for


embedded SQLScript
 The user can set a Background colour for better visibility AMDP
methods in the class.
 The User can access other AMDP methods, ABAP dictionary
view and ABAP tables in AMDP method.
 AMDP method are called like other regular ABAP methods.
 User can perform detailed analysis of various error during runtime
in transaction ST22
 Modification or Enhancement of regular ABAP classes can be done
by Users.
Lets Take one Example: Creating AMDP with Input, Output
Parameters

First Go to ABAP modeling perspective in HANA Studio

Windows->Perspective->Open Perspective->ABAP   ( )
Create ABAP class:
CLASS ztestk DEFINITION public.

PUBLIC SECTION.

types : tt_mara type table of mara.

interfaces : if_amdp_marker_hdb.

methods : my_method

importing value(im_matnr) type mara-matnr

exporting value(et_mara) type tt_mara.

ENDCLASS.

CLASS ztestk IMPLEMENTATION.

method my_method by database procedure for HDB

language sqlscript options read-only using MARA.

et_mara=SELECT * from MARA where MATNR= IM_MATNR;


endmethod.

ENDCLASS.

Save It(Control+S)

Open SAP GUI

Enter TCODE : SE24 (To see u r class is created or not)

We can see our methods and code also.

Click on Display
Enter TCODE : SE38 (To Create report )

In Report we are calling class (creating object to class)

Click on create
Type u r code here to call class

REPORT ZTESTK_REP.
PARAMETERS : p_matnr TYPE matnr DEFAULT ‘000000000000001109’.

DATA : r_amdp TYPE REF TO ztestk,
et_mara TYPE TABLE OF mara,
r_salv TYPE REF TO cl_salv_table.

CREATE OBJECT r_amdp.

r_amdp->my_method( EXPORTING im_matnr  = p_matnr
IMPORTING et_mara    = et_mara ).
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table   = r_salv
CHANGING
t_table        = et_mara
.
CATCH cx_salv_msg .
ENDTRY.

r_salv->display( ).

————————————————————————

Finally press F8 Button to execute


Step-by-Step procedure for creation, execution and storing of ABAP
Managed Database Procedures in HANA

ABAP Managed Database Procedure


Database Procedures are stored and executed in the Database. We can create
and execute database procedures in HANA database through ABAP using
AMDP Class and AMDP Method called ABAP Managed Database
Procedures. SQL SCRIPT is the language for creating stored procedures in
HANA. Main benefit of using SQL Script is to allow the execution of
complex calculations inside HANA database. The language is varies from
one database system to another.The ABAP Managed Database procedures
should be created using ABAP Development Tools (Eclipse or HANA
Studio).

Creation of ABAP Managed Database Procedure in ABAP


1. Open ABAP Development Tool (Eclipse or HANA studio) and Go to
ABAP Perspective.           Create new ABAP Class.

2. Provide Name and Description. Click on NEXT Button.


3. Click on Finish button.

4. AMDP Class Definition


An AMDP is implemented in an AMDP class with a regular static method or
instance method in any visibility section. The editing environment for AMDP
is the ABAP class editor.
The AMDP class must contain the appropriate tag
interface. IF_AMDP_MARKER_HDBis Marker Interface for DB
Procedures.
Example:
a. In Class Definition provide interface IF_AMDP_MARKER_HDB.
b. Define the table type TT_ORDER and structure type TY_ORDER.
c. Define the method GET_SALESORDER_DETAILS (Method
parameters should be Passed       by value).
Logic:

CLASS zcl_salesorder_details DEFINITION
 PUBLIC
 FINAL
 CREATE PUBLIC.
PUBLIC SECTION.
*Marker interface for Database Procedures
 INTERFACES: if_amdp_marker_hdb.
*Structure
 TYPES:
 BEGIN OF ty_order,
   vbeln      TYPE vbeln,    “Sales Order Number
   posnr      TYPE posnr_va, “Item Number
   vkorg      TYPE vkorg,    “Sales Organization
   item_price TYPE netwr_ap, “Item Price
   status         TYPE char30,   “Delivery Status
 END OF ty_order.
* Table type
 TYPES:
  tt_order TYPE STANDARD TABLE OF ty_order WITH EMPTY KEY.
* Method Definition
 CLASS-METHODS get_salesorder_details
    IMPORTING
      VALUE(iv_vbeln) TYPE vbeln
    EXPORTING
      VALUE(et_order) TYPE tt_order.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

5. Implementaion of AMDP Method in AMDP Class


Logic:

CLASS zcl_salesorder_details IMPLEMENTATION.
METHOD get_salesorder_details BY DATABASE PROCEDURE
                             FOR HDB
                             LANGUAGE SQLSCRIPT
                             OPTIONS READ–ONLY
                             USING vbak vbap vbup.
*To get Sales Order details
et_order = SELECT vbak.vbeln,
                  vbap.posnr,
                  vbak.vkorg,
                  vbap.netwr as item_price,
                  CASE LFSTA
                       WHEN ‘ ‘ then ‘Not Relevant’
                       WHEN ‘A’ then ‘Not yet processed’
                       WHEN ‘B’ then ‘Partially processed’
                       WHEN ‘C’ then ‘Completely processed’
                  END AS status
            FROM vbak AS vbak INNER JOIN vbap AS vbap
                  ON vbak.vbeln = vbap.vbeln
            INNER JOIN vbup AS vbup
                  ON vbup.vbeln = vbap.vbeln AND vbup.posnr = vbap.posnr
            WHERE vbak.vbeln = iv_vbeln;ENDMETHOD.
ENDCLASS.

6. Activate the AMDP Class and Check the created class in Transaction
SE24.

Execute the ABAP Managed Database Procedure through Report


1. Create a New ABAP Program.

2. Provide Name and Description. Click on NEXT button.


3. Click on Finish button.
4. Call the AMDP Method in ABAP editor.

Logic:

REPORT zr_call_amdp.
PARAMETER p_vbeln TYPE vbeln.
* To Call AMDP Method
zcl_salesorder_details=>get_salesorder_details(
                EXPORTING iv_vbeln = p_vbeln
                IMPORTING et_order = data(lt_order) ).
* To display Sales Order Details
cl_demo_output=>display_data( name = ‘Sales Order Details’
                              value = lt_order ).

Output:
Provide the sales order number as the input
Stored in HANA Database
1. Check the DB Connection in tcode DBACOCKPIT.

2. Database procedure will create in HANA DB at the first call of AMDP


Method .
3. Go to SAP HANA Development perspective –> HANA DB System –>
Catalog –>
    Schema –> Procedures.
    The AMDP Method Implementation will be stored as Database procedure
    and Table Types of AMDP Class also stored under Schema ‘SAPABAP1’.
4. The Table Type ‘TT_ORDER ‘ of AMDP Class will be stored as               
“ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS=
>P00000#ttyp”

5.The AMDP Method ‘GET_SALESORDER_DETAILS‘ of AMDP Class


   ‘ZCL_SALESORDER_DETAILS‘ will be stored as Database procedure
 
‘ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS’ a
s shown below.
Logic:
create procedure
  “ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS”
(
  in “IV_VBELN” NVARCHAR (000010),
 
out “ET_ORDER”“ZCL_SALESORDER_DETAILS=>GET_SALESORDE
R_DETAILS=>P00000#ttyp”
)
language sqlscript sql security invoker reads sql data as begin
–To get Sales Order details
  et_order = SELECT vbak.vbeln,
                    vbap.posnr,
                    vbak.vkorg,
                    vbap.netwr as item_price,
                    CASE LFSTA
                        WHEN ‘ ‘ then ‘Not Relevant’
                        WHEN ‘A’ then ‘Not yet processed’
                        WHEN ‘B’ then ‘Partially processed’
                        WHEN ‘C’ then ‘Completely processed’
                    END AS status
             FROM “ZCL_SALESORDER_DETAILS=>VBAK#covw” AS vbak
      
INNER JOIN “ZCL_SALESORDER_DETAILS=>VBAP#covw” AS vbap
                        ON vbak.vbeln = vbap.vbeln
      
INNER JOIN “ZCL_SALESORDER_DETAILS=>VBUP#covw” AS vbup
                        ON vbup.vbeln = vbap.vbeln AND vbup.posnr =vbap.posnr
                        WHERE vbak.vbeln = iv_vbeln;
end;

6. ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS#
stb2#20160831121018              and
  
ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS#st
ub#20160831121018            are for calling Database procedure
“ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS”

Logic:
create procedure
 
“ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS#stb2
#20160831121018”
(
  in “IV_VBELN” NVARCHAR (000010)
)
language sqlscript sql security invoker reads sql data as begin
 
call “ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS” 
(
     “IV_VBELN” => :IV_VBELN ,
     “ET_ORDER” => :ET_ORDER
   );
   select * from :ET_ORDER;
end;

Logic:
create procedure
 
“ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS#stub
#20160831121018”
(
   in “IV_VBELN” NVARCHAR (000010)
)
language sqlscript sql security invoker reads sql data as begin
 
call “ZCL_SALESORDER_DETAILS=>GET_SALESORDER_DETAILS” 
(
   “IV_VBELN” => :IV_VBELN ,
   “ET_ORDER” => :ET_ORDER
  );
  select * from :ET_ORDER;
end;

7. The database tables VBAK VBAP and VBUP are used in AMDP Method
will be created as          VIEWS in HANA Database system.
i) ZCL_SALESORDER_DETAILS=>VBAK#covw
ii) ZCL_SALESORDER_DETAILS=>VBAP#covw

iii) ZCL_SALESORDER_DETAILS=>VBUP#covw

You might also like