ABAP Managed Database Procedure
ABAP Managed Database Procedure
AMDP Classes
AMDP Methods
AMDP Polymorphism
AMDP Calls
AMDP BAdIs
Notes
Programming Guideline
AMDP Classes
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
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.
Parameter names:
Note
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.
AMDP Calls
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
AMDP BAdIs
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.
Notes
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.
Examples
See AMDP - Examples.
Continue
METHOD - BY DATABASE PROCEDURE
AMDP - Exception Classes
AMDP - Examples
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
Syntax
Extras:
Effect
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
Examples
See AMDP - Examples.
Addition 1
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.
Notes
An AMDP class can contain multiple tag interfaces for different
database systems for implementing AMDP methods in various
languages.
Addition 2
Effect
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)
Addition 3
Effect
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.
Note
Addition 4
Effect
Declares the database objects managed by ABAP accessed in the AMDP
procedure. The following must be specified after USING:
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.
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.
Notes
Continue
AMDP - SQL Script for the SAP HANA Database
AMDP - L for the SAP HANA Database
Syntax
Parameter Interface
Calls
Notes
o Privilege Execute on the
object TRUNCATE_PROCEDURE_OBJECTS of the schema SYS
Syntax
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
Parameter Interface
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
Numeric Types
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
Calls
Examples
See AMDP - Examples.
Source Code
REPORT demo_amdp.
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.
START-OF-SELECTION.
demo=>main( ).
Description
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.
Source Code
REPORT demo_amdp_call_amdp_procedure.
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.
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:
Source Code
REPORT demo_amdp_call_db_procedure.
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.
START-OF-SELECTION.
demo=>main( ).
Description
Source Code
REPORT demo_amdp_polymorphism.
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.
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.
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.
Source Code
REPORT demo_amdp_changing.
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
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.
Source Code
REPORT demo_amdp_connection.
cl_demo_output=>display( result ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
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.
Source Code
REPORT demo_amdp_mesh.
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.
START-OF-SELECTION.
demo=>main( ).
Description
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.
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.
Source Code
REPORT demo_amdp_l_language.
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
Note
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.
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.
PUBLIC SECTION.
INTERFACES IF_AMDP_MARKER_HDB. //Marker Interface for
HANA DB//
ENDCLASS.
METHODS process
// Write ABAP source code here//
...
ENDMETHOD
Windows->Perspective->Open Perspective->ABAP ( )
Create ABAP class:
CLASS ztestk DEFINITION public.
PUBLIC SECTION.
interfaces : if_amdp_marker_hdb.
methods : my_method
ENDCLASS.
ENDCLASS.
Save It(Control+S)
Click on Display
Enter TCODE : SE38 (To Create report )
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( ).
————————————————————————
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.
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.
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.
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