Reading Sample Sap Press Abap Development For Sap Hana
Reading Sample Sap Press Abap Development For Sap Hana
Contents
Index
The Authors
www.sap-press.com/4954
Chapter 6
ABAP-Managed Database Procedures
ABAP-managed database procedures can be leveraged to optimize non-
native, ABAP-based applications such as programs, forms, and interfaces
by leveraging the code pushdown features supported by SAP HANA 6
database. In this chapter you will learn to define and implement AMDP
methods.
In Chapter 2, we briefly introduced you to the code pushdown paradigm, which helps
developers optimize business applications on the SAP HANA database. This chapter
will include an emphasis on ABAP-managed database procedures (AMDPs), one of the
recommended approaches to achieve code pushdown functionalities.
In this chapter, we’ll start by covering essential aspects like the motivation behind
introducing AMDPs, including how they are created and consumed in business appli-
cations in Section 6.1. We’ll then show you how to create AMDP classes in Section 6.2
and also explore the concepts behind enhancements using AMDPs in Section 6.3.
Next, we’ll turn to exception handing in Section 6.4 and explore different debugging
tech- niques to analyze your procedures in Section 6.5. We’ll conclude this chapter on
AMDPs tools in Section 6.6.
6.1 Introduction
Let’s briefly recap some information you learned earlier in this book about code push-
down. In the classic style of coding, as shown in Figure 6.1, developers used to design
applications by retrieving all the data at once using database array operations such as
FOR ALL ENTRIES, JOIN, or ABAP Dictionary views. This approach reduced loads on data-
base servers by limiting data transfer requests between the application and the data-
base server.
Subsequent data processing operations are performed on the application server’s
internal table to achieve the desired results. However, this approach’s drawback was
the formation of complex SQL queries to retrieve data. Performance issues arose
because unnecessary data was retrieved, filtered, and processed in the application
layer.
159
6 ABAP-Managed Database Procedures 6.1 Introduction
view changed. These shortcomings were later handled with the release of SAP Net-
Classical Database New Approach
Weaver AS ABAP 7.4 SP05 by introducing progressive code pushdown techniques like
ABAP-managed database procedures (AMDPs) and core data services (CDS). These tech-
Presentation Layer niques are referred to as top-down approaches because the entire lifecycle manage-
Presentation Layer ment is conducted by the ABAP layer. SAP HANA artifacts such as views and
Data-to-Code
Data Operations (Calculations, Aggregations) procedures are automatically created in the database.
Database Layer Database Layer < 7.4 SP02 > 7.4 SP02 > 7.4 SP05
SAP HANA ABAP
transport container AMDP CDS transport (CTS)
Oracle or any database SAP HANA database Native SQL Proxy objects
AS
Created on first method call
Figure 6.1 New Programming Approach
SAP
and data processing to the database is called the code-to-data paradigm, shown in
Figure 6.1, instead of the classic data-to-code approach.
Package
In SAP HANA, several code-to-data techniques are available for implementing data-
intensive calculations in the database layer. SAP HANA performs data-intensive calcu-
lations in the database layer using SAP HANA views or procedures. These artifacts are Figure 6.2 Code-to-Data Approach
later utilized in ABAP applications using several code-to-data techniques. Whether or
not you should use these techniques depends on the SAP NetWeaver Application For example, when an AMDP class and method are defined, a procedure is created in
Server for ABAP (AS ABAP) release used in your landscape. the database before an AMDP method is called for the first time in the calling program
As shown in Figure 6.2, if you are using SAP NetWeaver AS ABAP 7.4 SP02 or lower, SAP and updated on the subsequent calls if it has been changed. In contrast, in the case of
HANA repository objects, such as SAP HANA views or procedures, are directly a CDS view, the SAP HANA view is created when the CDS view is activated.
accessed in ABAP applications using native SQL. With SAP NetWeaver AS ABAP 7.4
SP02 or higher, these objects are accessed in ABAP applications using ABAP proxy 6.1.1 ABAP-Managed Database Procedure Framework
objects such as external views or database procedure proxies to overcome the limitations
AMDPs are a recommended technique for achieving code pushdown functionality if
of using native SQL.
your underlying database is SAP HANA. The framework uses a top-down approach to
The techniques used before SAP NetWeaver AS ABAP 7.4 SP05 are known as bottom- create and manage database procedures in the ABAP environment. The AMDP frame-
up approaches because SAP HANA views or procedures are created in the database work guarantees that the complete lifecycle of the AMDP procedure—from creating,
layer. The views are created using a database user in the Modeler perspective and later changing, activating, and transporting the procedure—occurs in the application layer
con- sumed in the ABAP layer using Native SQL or proxy objects. Although these by the ABAP runtime environment.
techniques offer the benefit of performing the data processing in the database layer,
As a developer, you’ll write procedures in an AMDP method implementation of an
one limitation of using a bottom-up approach is its complex lifecycle management
AMDP class. In contrast to the traditional ABAP method, an AMDP method is a
requirements. For example, handling SAP HANA repository objects must occur
unique method and implements database-specific programming languages, such as
separately through deliv- ery units and ABAP proxy objects by using the SAP HANA
SQLScript, native SQL, and L (used internally by SAP). The keyword LANGUAGE specifies
transport containers to import objects to other systems. Additionally, developers must
the database- specific language for implementing the procedure.
ensure proper synchro- nization of all SAP HANA artifacts in all the environments
whenever any procedure or
161
160
6 ABAP-Managed Database Procedures 6.1 Introduction
As shown in Listing 6.1, the usage of the keyword BY DATABASE PROCEDURE in the imple- ■ Currently, the AMDP framework only supports database procedures for SAP HANA
mentation section of the AMDP method helps differentiates whether the method uses databases. However, SAP has designed the framework to support stored procedures
ABAP or any other language to implement the procedure.
for other databases.
AMDP Class Definition ■ You can only create or edit an AMDP using the Eclipse-based ABAP Development
CLASS zcl_amdp_example DEFINITION Tools (ADT). Thus, the classic SAP GUI-based class builder (Transaction SE24) is not
PUBLIC
suitable for managing an AMDP class and its methods, as only the display function
FINAL
CREATE PUBLIC. is supported in Transaction SE24. 6
■ Developers classified as standard ABAP users with appropriate authorizations can
PUBLIC SECTION. manage database procedures using an AMDP class, and Transaction SICK can
detect missing permissions.
AMDP Marker Interface
INTERFACES if_amdp_marker_hdb.
Benefits of Using AMDPs
AMDP Method 6.1.2 Development Environment for AMDP
CLASS-METHODS amdp_method Let’s discuss a few benefits of using AMDPs:
Importing parameter defined using Dictionary Using
■ AABAP Development
standard ABAP userTools (ADT) isanmandatory
can manage for creating
AMDP, unlike and changing
in bottom-up techniquesAMDPs,
like
type IMPORTING VALUE(im_input) TYPE matnr as the SAP
classic
HANA views and procedures, where both ABAP and SAP HANAfunction.
SAP GUI-based Transaction SE24 only supports the display databaseADT
Importing parameter defined using ABAP type
users are required.
EXPORTING VALUE(ex_output) TYPE i
Importing parameter defined using TABLE type ■ The AMDP framework is responsible for communicating with the database
CHANGING VALUE(ch_param) TYPE ttyp_d. and automatically creating the database procedures as SAP HANA repository163
catalog objects.
PROTECTED SECTION.
■ The entire lifecycle management to synchronize, create, change, activate, and
PRIVATE SECTION.
transport procedures is performed in the ABAP layer.
ENDCLASS.
■ The ABAP perspective within ADT serves as a development environment for
AMDP Class Implementation writing and managing your SQL scripts.
CLASS zcl_amdp_example IMPLEMENTATION. ■ The framework supports full integration of SQLScript syntax check and
debugging into the ABAP environment.
AMDP Method Implementation
■ Even though an AMDP might be implemented using a database-specific
METHOD amdp_method BY DATABASE PROCEDURE
FOR HDB language, such as native SQL or SQLScript, the ABAP environment still evaluates
LANGUAGE SQLSCRIPT. source code for any syntax errors.
--Implement SQLScript Code ( Database specific code ) ■ Procedures are automatically created in the SAP HANA database by the ABAP
ENDMETHOD. run- time environment before the first AMDP method call.
ENDCLASS.
■ You can extend an AMDP using Business Add-Ins (BAdIs) if it has an extension
Listing 6.1 AMDP Framework Definition pro- visioned by the software provider.
■ An AMDP is not a replacement for database procedure proxies, which are still
In general, you should consider the following points before creating or consuming an con- sidered in sidecar scenarios with secondary database connections to access
AMDP: SQL- Script procedures in a different SAP HANA database.
162
6 ABAP-Managed Database Procedures 6.1 Introduction
also delivers additional features for developers to work effectively with AMDPs and Select General • Appearance • Colors and Fonts • ABAP, as shown in Figure 6.5. Then,
improve developer productivity and efficiency. select Embedded language under Syntax Coloring and click Edit... to modify the color
Some features delivered by ADT for managing ADMPs include the following: according to your preferences.
In ADT, SQLScript syntax error is fully supported and integrated into ABAP. This can be
seen by toggling the cursor on the error marker on the right-hand side of the form-
based editor. The detailed SQLScript syntax errors can be seen by toggling the cursor
on the error, as shown in Figure 6.6.
In ADT, the appearance of the form-based editor can be changed by the developer to
highlight syntax errors and to differentiate between the embedded (database-specific)
language and the ABAP language. To enable syntax highlighting, navigate to Win-
dows • Preferences, as shown in Figure 6.4.
In ADT, you can also highlight the SQLScript syntax errors at the point where they
occur. As shown in Figure 6.7, the syntax error statement SFLIGHT is emphasized in
amber color. Additionally the detailed syntax error description can also be seen on the
right-hand side of the source code editor.
164 165
6 ABAP-Managed Database Procedures 6.2 Creating AMDP Classes
code shown in Listing 6.2 illustrates a simple AMDP definition implementing all the
required prerequisites. These prerequisites are as follows:
■ An AMDP class definition should contain a marker interface IF_AMDP_MARKER_HDB, as
it implements an AMDP method for the SAP HANA database.
■ In the class definition, the AMDP method parameter types should be a dictionary,
ABAP (for example, integer or character), or table types. For parameters with table
types, the line types should contain elementary components because nested tables 6
are not supported.
■ An AMDP method can only contain importing, exporting, and changing
parameters. An AMDP method cannot have return parameters.
■ Similar to remote function call (RFC) parameters, all method parameters should be
Figure 6.7 Emphasizing SQLScript Errors and Differentiating between ABAP and Database defined as pass by value. Pass by reference in the method definition is not permitted.
Specific Code
■ An AMDP method can be defined in the PUBLIC SECTION, PRIVATE SECTION, or
PROTECTED SECTION of the class. However, if the AMDP methods of other classes do
In the AMDP method, you can also set the background color of the embedded
not call the method, you must declare the method as PRIVATE.
language to differentiate between ABAP and database specific code such as SQLScript.
Set the color using the same process we discussed earlier in Figure 6.4: navigate to CLASS zcl_amdp_demo_01 DEFINITION
Windows • Preferences • General • Appearance • Colors and Fonts • ABAP • Embedded PUBLIC
FINAL
language. Figure 6.7 you can see that the background color for database-specific
CREATE PUBLIC.
syntax is empha- sized in gray color.
PUBLIC SECTION.
A global class must be defined in the class library using ADT to create an AMDP Type Declaration
proce- dure. A class is categorized as an AMDP class if its definition contains one or TYPES: BEGIN OF d_flight,
carrid TYPE s_carr_id,
more tag interfaces. The tag interfaces are prefixed with IF_AMDP_MARKER and end in a
connid TYPE s_conn_id,
suffix indi- cating database system for which the procedure is implemented.
bookamt TYPE s_f_cur_pr,
In the following sections, we will understand several prerequisites that you should loccurkey TYPE s_currcode,
con- sider while defining and implementing an AMDP method. You will also learn to END OF d_flight.
con- sume AMDP and check if current database (or a database specified using a TYPES: tt_flight TYPE STANDARD TABLE OF d_flight.
database connection) supports the AMDP features in the ABAP applications.
AMDP Method Definition
CLASS-METHODS get_flight_data
IMPORTING
Example
VALUE(iv_filters) TYPE string
6.2.1 Prerequisites
The marker interface IF_AMDP_MARKER_HDB is relevant for SAP HANA database, where VALUE(iv_client) TYPE sy-mandt
An HDB
AMDP class that
indicates can be
thecomprised ofintended
procedure is one or more
for antraditional
SAP HANA and AMDP methods. It
database. EXPORTING
can also contain AMDPs for each database system specified by a tag interface. The VALUE(e_flight) TYPE tt_flight
source
166
167
6 ABAP-Managed Database Procedures 6.2 Creating AMDP Classes
CHANGING
CLASS zcl_amdp_demo_01 IMPLEMENTATION.
VALUE(c_return) TYPE i METHOD get_flight_data BY DATABASE PROCEDURE
FOR HDB
RAISING cx_amdp_no_connection LANGUAGE SQLSCRIPT
cx_amdp_execution_error. OPTIONS READ-ONLY
PROTECTED SECTION. USING sflight sbook.
PRIVATE SECTION.
ENDCLASS. Data selection from data sources using SQLScript 6
Listing 6.2 AMDP Class Definition E_FLIGHT = SELECT a.carrid,
a.connid,
sum(loccuram) AS bookamt,
6.2.2 Implementing AMDP Methods b.loccurkey
FROM sflight AS a INNER
An AMDP method is a unique method that optimizes ABAP applications by imple- JOIN sbook AS b
menting code pushdown from the application server layer to the database layer. This ON a.carrid = b.carrid
method is wrapped in a global class and can be defined as either a static method or AND a.connid = b.connid
instance method. Even though you can define an AMDP method as an instance WHERE a.mandt =
:iv_client
method, it will always be executed as a static method call.
GROUP BY A.carrid, a.connid, b.loccurkey;
Two types of AMDP methods exist:
Filter based on the selection screen criteria
■ An AMDP procedure without a return code is defined by a method using the addi-
E_FLIGHT = APPLY_FILTER( :E_FLIGHT, :iv_filters );
tion BY DATABASE PROCEDURE.
■ An AMDP function with a return code is defined by a method using the addition BY ENDMETHOD.
DATABASE FUNCTION. ENDCLASS.
Any regular method within an AMDP class can be transformed into an AMDP method Listing 6.3 AMDP Method Implementation Example
by using either the BY DATABASE PROCEDURE or BY DATABASE FUNCTION addition at the
start of the method statement in the method implementation part, followed by the Listing 6.3 contains the following elements:
database system for which the procedure is implemented, the language in which the ■ A global AMDP class implementation contains the AMDP method implementation.
business logic is written, and the mandatory ABAP objects (which may include In our example, our AMDP method will summarize total flight sales by airline and
transparent tables, views, and other AMDPs that are used as data sources). flight code.
Additionally, you can mark an AMDP method as READ ONLY using the addition OPTIONS, ■ The method GET_FLIGHT_DATA is implemented as an AMDP method since the method
which is optional. is defined with the addition BY DATABASE PROCEDURE.
The body of an AMDP method, shown in Listing 6.3 uses database-specific language ■ What follows is the database addition HDB to implement the procedure for the SAP
such as SQLScript or native SQL. The source code shown in Listing 6.3 illustrates an HANA database. The AMDP framework only supports SAP HANA database; however,
AMDP class and method with database-specific logic using SQLScript to summarize the framework is designed to work with other databases.
sales by airline and flight code. The procedure also filters records based on the ■ Further, the database-specific language to be used is specified. In this case, SQLScript
selection screen using the FILTER keyword. To implement business logic, you can use will be used in the AMDP method to implement the business logic.
the full SQLScript reference, except for calculation engine (CE) functions such as the ■ All database objects, such as dictionary tables, views, and other AMDP methods
following: used as data sources within the method body, must be declared explicitly with the
■ CE_LEFT_OUTER_JOIN key- word USING. These objects can be accessed directly, that is, without the need to
■ CE_COLUMN_TABLE prefix these objects with SAP<SID> (Schema). However, for nested AMDP calls, that
is, for
■ CE_UNION_ALL
168 169
6 ABAP-Managed Database Procedures 6.2 Creating AMDP Classes
AMDP methods called inside an AMDP body, you should specify objects by their
full names, that is, with the class they belong to and the method name in uppercase
and closed in double quotation marks.
■ You must ensure that any objects that are not part of SAP<SID> schema are available
at runtime since these objects are not managed and therefore are not included in
the USING clause.
■ Finally, the SQLScript language is used within the AMDP body to write the business
6
logic that is executed in the database layer.
Figure 6.8 Consuming AMDP Method in ABAP (Eclipse)
■ The procedure results are filtered using the SQLScript function APPLY_FILTER based
on the selection criteria provided as an importing parameter to the AMDP method.
■ The procedure written in the AMDP body is highlighted in gray to differentiate
between ABAP-specific language and database-specific language. Refer to Figure
6.4 to see how to set the color for the editor’s background.
You should consider the following restrictions when implementing an AMDP method:
■ Data definition language (DDL) such as Create, Alter, or Delete are not allowed to
create, change, or delete any database objects.
■ You cannot access local temporary data objects, such as internal tables, or variables
defined in the class definition in the method implementation.
■ Statements like database commits and rollbacks are not allowed in the method
body. Also, to avoid data inconsistencies between procedures, you should handle Figure 6.9 Consuming AMDP Method in ABAP (SAP GUI)
logical units of work separately in the ABAP program.
■ You cannot extend AMDP methods using implicit enhancement options, as these However, to consume an AMDP, the SAP NetWeaver AS ABAP’s central database should
methods are directly executed on the database, and implicit options are not avail- be managed by the database system for which the AMDP method is implemented. If
able within an AMDP method. not the case, then the procedure call results in a runtime error.
■ While using data manipulation language (DML), such as INSERT, UPDATE, MODIFY, As shown in Figure 6.10, before the first method call, the ABAP runtime environment
DELETE, etc., write access to buffered tables is not allowed. creates the procedure implemented in the AMDP method in the database system
or updates any existing database procedure if the AMDP has changed, as shown in
Figure
6.2.3 Calling AMDP Methods in Applications 6.11. Once the method is called, the execution is performed in the database system.
An AMDP method is called in an ABAP application similar to any other regular Parameters of the interface are passed from the native SQL interface to the database
method, using an Eclipse-based form editor in ADT or through SAP GUI-based system or are applied by the database system.
transactions. These methods are always executed as static method calls, even if
defined as instance methods.
You can call an AMDP method in an ABAP application in several ways. In the Eclipse-
based ABAP perspective (see Figure 6.8), you can use the code completion template by
pressing (Ctrl) + (Space) to call an AMDP method. In SAP GUI-based editors (see
Figure 6.9), you can use ABAP-based patterns by pressing (Ctrl) + (F6).
170 171
6 ABAP-Managed Database Procedures 6.2 Creating AMDP Classes
" Variable 6
DATA: gv_return TYPE i.
" Build dynamic where clause, and pass it to the AMDP method
Figure 6.11 AMDP Procedure Created on the Database
TRY.
DATA(lv_where_clause) = cl_shdb_seltab=>combine_seltabs
Once a database procedure managed using AMDP has been created (ZCL_AMDP_DEMO_01 (it_named_seltabs = VALUE #(
=>GET_FLIGHT_DATA) on the database schema, SAPABAP2, as show in Figure 6.11, this proce- ( name = 'CARRID' dref = REF #( s_carrid[] ) )
dure can be called from other database procedures using the database syntax, ( name = 'CONNID' dref = REF #( s_connid[] ) ) ) ).
provided that the database permits this access, including AMDP procedures (or CATCH cx_shdb_exception INTO DATA(lref_shdb_exception).
DATA(lv_meesage) = lref_shdb_exception->get_text( ).
database proce- dures) that are not managed by AMDP. If an AMDP procedure calls
ENDTRY.
another procedure, this procedure must be specified in the calling method with the
addition USING.
In general, we recommend that AMDP procedure implementations that are not called " AMDP Method Call
TRY.
from AMDP methods of other classes be created as private methods of an AMDP class
and that they be called in regular ABAP methods.
zcl_amdp_demo_01=>get_flight_data( EXPORT
ING
Note iv_filters = lv_where_clause
iv_client = sy-mandt
In database systems that do not support AMDP, a traditional method can be created
IMPORTING
using aninalternative
As shown implementation
Listing 6.4, in Open SQL
an ABAP application can or native
call SQL. procedure to display
an AMDP e_flight = gt_flight
flight booking information based on a user’s selection. The example also illustrates the CHANGING
use of SELECT-OPTIONS to filter data records. c_return = gv_return ).
" Error Handling
REPORT zcl_amdp_demo_call_01. CATCH cx_amdp_no_connection INTO DATA(lref_no_connection).
DATA(lv_error) = lref_no_connection->get_text( ).
" Data declaration CATCH cx_amdp_execution_error INTO DATA(lref_amdp_execution_error).
DATA: gwa_sflight TYPE sflight. lv_error = lref_amdp_execution_error->get_text( ).
ENDTRY.
" Select Options
SELECT-OPTIONS: s_carrid FOR gwa_sflight-carrid, " Display results
s_connid FOR gwa_sflight-connid. IF lv_error IS INITIAL.
cl_demo_output=>display_data(
" Types Declaration EXPORTING
TYPES: BEGIN OF d_flight, value = gt_flight
carrid TYPE s_carr_id, name = 'Flight Booking information').
connid TYPE s_conn_id,
172 173
6 ABAP-Managed Database Procedures 6.2 Creating AMDP Classes
" Error Handling An ABAP report can consume an AMDP method, as shown in Listing 6.5, filtering data
ELSE.
based on the user selection via parameters or based on SELECT-OPTIONS to display
WRITE: lv_error.
ENDIF. book- ings for all airline codes by a specific date and customer category.
* Data declaration
6.2.4 Using Multiple Selection Criteria DATA: gwa_sflight TYPE sflight. 6
In ABAP reports, defining a selection screen is essential to empowering business users
* Selection screen
so they can filter data based on the desired elements. Selection criteria ensure that
PARAMETERS: p_date TYPE s_date.
applications can process data faster by filtering out unwanted data in the database
layer. But, to filter the data, you must define selection criteria using parameters, SELECT-OPTIONS: s_carrid FOR gwa_sflight-carrid,
SELECT-OPTIONS, or a combination of both. s_connid FOR gwa_sflight-connid.
The purpose of parameters is to filter the records based on a single value, whereas
* Build dynamic where clause
with SELECT-OPTIONS, you can define complex selection criteria to filter out records.
TRY.
Develop- ers can then use these selection screen elements directly in a WHERE clause of DATA(lv_where_clause) = cl_shdb_seltab=>combine_seltabs(
an Open SQL statement to filter the data. These selection criteria are then converted it_named_seltabs = VALUE #(
into the SQL WHERE conditions by the ABAP application server. ( name = 'CARRID' dref = REF #( s_carrid[] ) )
However, suppose you want to use these selection screen elements in an AMDP proce- ( name = 'CONNID' dref = REF #( s_connid[] ) )
)
dure. In this case, you can use parameters directly in the AMDP method, but this
iv_client_field = 'MANDT' ).
approach is not valid with SELECT-OPTIONS.
CATCH cx_shdb_exception INTO DATA(lref_shdb_exception).
Because you cannot pass SELECT-OPTIONS directly to an AMDP method, this limitation DATA(lv_meesage) = lref_shdb_exception->get_text( ).
of using an AMDP must be kept in mind. To pass SELECT-OPTIONS to an AMDP method, ENDTRY.
you must first transform the selection criteria into a filter string and then pass the
string as an IMPORTING parameter to the AMDP method. To convert the SELECT-OPTIONS * AMDP Method call to summarize booking amount by flight, airline code,
(selection tables or range tables) into a dynamic SQL WHERE clause, you can use the static date, and customer type
zcl_amdp_sflight_details=>get_data( E
method COMBINE_SELTABS( ) of the new class CL_SHDB_SELTAB.
XPORTING
This generated condition can then be used in SQLScript to filter the data source using iv_client = sy-mandt
the SQLScript function APPLY_FILTER in the AMDP method implementation. This func- iv_date = p_date
tion can be applied to database tables, views, and SAP HANA views, however this iv_filters = lv_where_clause
IMPORTING
func- tion cannot be used with analytical or table variables.
et_results = DATA(gt_results) ).
The APPLY_FILTER function expects two parameters. The first parameter is the data
source to which you want to apply the filter, and the second parameter is the * Display results
generated WHERE clause, which is passed as a string argument. cl_demo_output=>display_data(
EXPORTING
The CL_SHDB_SELTAB class is not available with SAP NetWeaver AS ABAP 7.4 and
value = gt_results
should be imported by following the steps described in SAP Note 2124672. SAP
name = 'Flight Booking information').
NetWeaver AS ABAP 7.4 SP08 or higher is required to apply this SAP Note.
Listing 6.5 ABAP Report: Handling SELECT-OPTIONS
Note
The class CL_LIB_SELTAB and its methods are obsolete.
174 175
6 ABAP-Managed Database Procedures 6.2 Creating AMDP Classes
The AMDP method shown in Listing 6.6 is filtering the data based on parameters and WHEN 'P' then 'Private Customer'
SELECT-OPTIONS passed from the application program shown earlier in Listing 6.5 using ELSE 'Others'
the SQLScript function APPLY_FILTER. END AS "TYPE",
SUM(b.loccuram) AS TOTAL
CLASS zcl_amdp_sflight_details DEFINITION
from sflight as a INNER JOIN
PUBLIC
sbook as b on a.carrid = b.carrid
FINAL
and a.connid = b.connid
CREATE PUBLIC. 6
WHERE a.mandt = :iv_client --Parameters
AND b.fldate = :iv_date --Parameters
PUBLIC SECTION.
GROUP BY a.carrid, a.connid, b.fldate, b.custtype
* AMDP Marker Interface
;
INTERFACES: if_amdp_marker_hdb.
* Data declaration
* Filter based on Selection screen (Select options)
TYPES: BEGIN OF d_sflight,
ET_RESULTS = APPLY_FILTER( :ET_RESULTS, :iv_filters );
carrid TYPE s_carr_id,
ENDMETHOD.
connid TYPE s_conn_id,
ENDCLASS.
fldate TYPE s_date,
type TYPE string, Listing 6.6 AMDP: Filtering Using SELECT-OPTIONS and Parameters
total TYPE s_l_cur_pr,
END OF d_sflight,
6.2.5 Feature Support Check Using Global Classes
tty_sflight TYPE STANDARD TABLE OF d_sflight.
We recommend checking whether the current database or a database specified using a
* AMDP Method
CLASS-METHODS get_data database connection supports AMDP features and if it can be used at runtime in the
IMPORTING ABAP applications.
VALUE(iv_client) TYPE sy-mandt
The method USE_FEATURE of the global class CL_ABAP_DBFEATURES can be used to check
VALUE(iv_date) TYPE s_date
support for the database-specific feature. Several constants are provided to check data-
VALUE(iv_filters) TYPE string
EXPORTING base-specific features and can be passed to the USE_FEATURE method in an internal table.
VALUE(et_results) TYPE tty_sflight. The method returns the value of ABAP_TRUE if the feature is supported by the database,
PROTECTED SECTION. whereas unsupported values raise an exception from the class CX_ABAP_INVALID_PARAM_
PRIVATE SECTION. VALUE and can be handled within the application program to avoid runtime errors. The
ENDCLASS. database-specific features listed in Table 6.1 can be validated using the global class CL_
ABAP_DBFEATURES.
CLASS zcl_amdp_sflight_details IMPLEMENTATION.
METHOD get_data BY DATABASE PROCEDURE Database Feature Constant Name Value
FOR HDB
External views EXTERNAL_VIEWS 2
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY Maximum number of key fields > 16 (120) TABLE_KEYCNT_MAX1 3
USING sflight sbook.
Maximum width of key fields > 900 bytes TABLE_KEYLEN_MAX1 4
ET_RESULTS = SELECT a.carrid, a.connid, b.fldate, (up to 2000)
CASE b.custtype Maximum width of table or view > 4030 bytes TABLE_LEN_MAX1 5
WHEN 'B' then 'Business Customer' (up to 16293)
177
6 ABAP-Managed Database Procedures 6.2 Creating AMDP Classes
Hierarchies HIERARCHIES 13
TRY.
DATA(lv_supported) =
cl_abap_dbfeatures=>use_ features( EXPORTING
requested_features =
VALUE #( ( cl_abap_dbfeatures=>call_amdp_method ) Figure 6.13 Select Database Features for AMBP Supportability Check
( cl_abap_dbfeatures=>amdp_table_function ) ) ).
CATCH cx_abap_invalid_param_value INTO DATA(lref_invalid_value). 3. The report displays the list of supported and unsupported features for the underly-
DATA(lv_message) = lref_invalid_value->get_text( ). ing database (see Figure 6.14).
ENDTRY.
IF lv_supported IS NOT INITIAL."ABAP_TRUE
WRITE:'Database specific feature', cl_abap_dbfeatures=>call_amdp_
method, 'is supported'.
ELSE.
WRITE lv_message.
ENDIF.
You can also use the standard program DEMO_DBFEATURES to validate if the current
data- base supports any database features before using them.
To validate database-specific features, follow these steps:
1. Execute the standard SAP program DEMO_DBFEATURES using Transaction SE38 (see
Figure 6.12).
Figure 6.14 Resulting List of Supported and Unsupported AMDP Features
178
179
6 ABAP-Managed Database Procedures 6.3 Enhancements
6.3 Enhancements In addition to implementing the methods of a normal BAdI as AMDP methods and
making these methods callable using CALL BADI, you can also create a special AMDP
Similar to classic ABAP extensions, where several enhancement techniques like user
BAdI.
exits, customer exits, business transaction events (BTE), and business add-ins (BAdI),
An AMDP BAdI is created in Transaction SE20 and is later called within an AMDP imple-
enhancement frameworks are available to perform modification-free extensions to
mentation, similar to other AMDPs. An AMDP BAdI is a BAdI that is categorized accord-
SAP applications. These enhancements frameworks include implicit and explicit
ingly in the BAdI Builder and meets the following prerequisites, which are shown in
enhancements or are BAdI-managed using enhancements spots.
Figure 6.16: 6
In the following sections, you’ll learn how to define, implement, and invoke AMDP
1. SAP has provided an Enhancement Spot (ES_PPH_READ_BADI, in our example) and a
BAdI calls within other AMDP methods to extend standard business functionality.
BAdI Definition (PPH_AMDP_READ_MRP_BADI, in our example).
2. An AMDP BAdI does not currently have any filters, as they are not supported, indi-
6.3.1 AMDP BAdI Overview cated by the unchecked Limited filter use box in the Usability section.
You can also extend an AMDP procedure if the software or extension provides for this 3. In its definition, the BAdI is categorized as an AMDP BAdI, as you can see in the
extensibility. As described in Table 6.2, AMDP BAdIs were introduced with SAP Net- Usability section.
Weaver AS ABAP 7.4 SP08 to allow for modification-free extensions. As shown in Figure 4. The software provider has provided the mandatory Fallback Class. In this example,
6.15, you could then consume these extensions to add or modify a business require- the fallback class is CL_PPH_AMDP_READ_MRP_BADI. Only an AMDP class can be provided
ment in the procedure. as a fallback class or implementation class.
Default fallback
implementation
180 181
6 ABAP-Managed Database Procedures 6.3 Enhancements
Figure 6.17 BAdI Method Declared as AMDP Method Figure 6.19 AMDP Method Implementation
Let’s consider an example of an AMDP BAdI. Let’s say we want an AMDP class that 3. The AMDP method EXECUTE is consumed in an ABAP application, as shown in Figure
determines a customer’s category based on the customer type, which requires an 6.20, to display the custom category classification (see Figure 6.21) before
extension to achieve a customer-specific business requirement. The class’s AMDP extending the AMDP BAdI definition.
method will be consumed in an ABAP application to display the customer’s category
classification by following these steps:
1. The AMDP class definition ZCL_AMDP_CUST_CLASSIFICATION, as shown in Figure 6.18,
determines the customer category based on the customer type. The business logic is
encapsulated in the AMDP BAdI definition in the AMDP method ZIF_RECLASSIFY_
CUSTOMERS~RECLASSIFY of the fallback class and is called in the EXECUTE method imple-
mentation of the AMDP class ZCL_AMDP_CUST_CLASSIFICATION.
2. In the AMDP method implementation EXECUTE, the AMDP BAdI method RECLASSIFY
is called to determine the customer category, as shown in Figure 6.19. The method
Figure 6.21 ABAP Application Output
182
183
6 ABAP-Managed Database Procedures 6.3 Enhancements
To extend the ADMP method shown in Figure 6.20, the software provider needs to pro- 3. Our AMDP BAdI method ZIF_RECLASSIFY_CUSTOMERS~RECLASSIFY will encapsulate the
vision an AMDP BAdI. In this case, BAdI definition ZBADI_RECLASSIFY_CUSTOMERS is avail- business logic written in SQLScript language, as shown in Figure 6.24, to classify and
able and encapsulated in the enhancement spot ZES_RECLASSIFY_CUSTOMER. You can determine the customer category.
view the AMDP BAdI definition in Transaction SE18. Let’s look at the definition more
deeply, especially the following aspects:
1. An AMDP BAdI definition ZBADI_RECLASSIFY_CUSTOMERS is provided by SAP to extend
the procedure and is encapsulated in the Enhancement Spot ZES_RECLASSIFY_CUS
6
TOMER, as shown in Figure 6.22.
184 185
6 ABAP-Managed Database Procedures 6.3 Enhancements
2. Create the enhancement implementation by providing a name and a short text and
then clicking on OK, as shown in Figure 6.26.
6
Figure 6.26 Create Enhancement Implementation
Figure 6.27 Name and Describe BAdI Implementation and Specify Implementing Class
4. Click on Save or use the shortcut (Ctrl + S) to save the BAdI implementation.
Figure 6.29 Extend AMDP Method
5. Click on the Implementing Class (see Figure 6.28) to extend the AMDP method
RECLASSIFY and incorporate the customer requirement using database-specific lan-
guage (SQLScript). The customer category will be determined in this step, with
'Privilege Customer' for customer type B and 'General Customer' for customer type
P, as shown in Figure 6.29.
6. Click on Save and Activate to activate the implementation. The active implementa-
tion is shown in Figure 6.30.
186 187
6 ABAP-Managed Database Procedures 6.3 Enhancements
188 189
6 ABAP-Managed Database Procedures 6.3 Enhancements
2. Choose the object type Enhancement Spot and click on Next to create the enhance- 4. Alternatively, you can also use Transaction SE20 to create the BAdI definition (see
ment spot, as shown in Figure 6.36. Figure 6.38).
5. Next, specify a name for the enhancement spot and maintain the Short Text and
Technology fields, as shown in Figure 6.39. To create the enhancement spot, click on
OK.
Figure 6.36 Create an Enhancement Spot using ADT in SAP HANA Studio
3. To create the enhancement spot, maintain the Object Name field and click on Next,
as shown in Figure 6.37.
Figure 6.39 Specify Name and Short Text for the Enhancement Spot Using Transaction SE20
6. Under the Enh. Spot Element Definitions tab, as shown in Figure 6.40, click on Create
BAdI to create the definition.
190 191
6 ABAP-Managed Database Procedures 6.3 Enhancements
7. Provide the BAdI a name and a short description and click on OK to create the 9. Click on the Interface node under BAdI Definitions and specify the BAdI interface
defini- tion, as shown in Figure 6.41. name ZIF_RECLASSIFY_CUSTOMERS and then click on Yes to create the BAdI interface,
as shown in Figure 6.43.
8. Under the Usability section, categorize the BAdI definition as an AMDP BAdI by
selecting the AMDP BAdI checkbox, as shown in Figure 6.42. You cannot classify the Figure 6.43 Create a BAdI Interface
BAdI as filter dependent as the AMDP BAdI does not support filter functionality.
10. Click on the interface name ZIF_RECLASAIFY_CUSTOMERS to define the AMDP method
RECLASSIFY in the BAdI interface, as shown in Figure 6.44.
11. In the BAdI interface, as shown in Figure 6.45, include the AMDP marker interface
IF_AMDP_MARKER_HDB for the database for which the procedure is to be created, In our
case, this is specific to the SAP HANA database.
192 193
6 ABAP-Managed Database Procedures 6.3 Enhancements
METHODS: reclassify
IMPORTING
VALUE(iv_client) TYPE sy-mandt
EXPORTING
VALUE(et_results) TYPE tt_results.
ENDINTERFACE.
*AMDP Method Implementation in the fallback Class ZCL_RECLASSIFY_CUSTOMER_DEF
Figure 6.46 Create a Fallback Class
METHOD zif_reclassify_customers~reclassify BY DATABASE PROCEDURE
195
194
6 ABAP-Managed Database Procedures 6.3 Enhancements
FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY 2. The BAdI call is invoked by specifying both the BAdI definition and the method
USING sbook. name in uppercase and with the => separator. As shown in Figure 6.49, the BAdI
call is enclosed in double quotation marks, and furthermore, importing, exporting,
and
et_results = select carrid, changing parameters are passed to the interface method.
connid,
custtype,
CASE custtype
WHEN 'B' then 'Business Customer' 6
WHEN 'P' then 'Private Customer'
ELSE 'Others'
END AS "CATEGORY"
FROM sbook
where mandt = :iv_client
order by carrid, connid;
Figure 6.49 BAdI Call in an AMDP Method
ENDMETHOD.
Listing 6.8 BADI Interface and AMDP Method Implementation 3. Finally, as shown in Figure 6.50, the AMDP BAdI call is invoked in an ABAP
applica- tion to derive the customer category classification.
* Type Definition
TYPES: BEGIN OF d_results,
Figure 6.48 BAdI Definition Usage in an AMDP Method carrid TYPE s_carr_id,
197
196
6 ABAP-Managed Database Procedures 6.4 Exception Handling
CALL "ZBADI_RECLASSIFY_CUSTOMERS=>RECLASSIFY" be handled in the calling program to avoid runtime errors during program execution.
(:IV_CLIENT, :ET_RESULTS );
Figure 6.51 shows the exception classes that can be handled within an AMDP method
ENDMETHOD.
to avoid runtime errors.
Listing 6.9 BAdI Method Call in an AMDP Method
CX_RO
As shown in Listing 6.10, an AMDP BAdI method call in any application is similar to a
kernel BAdI call using a CALL BAdI or GET BAdI statement. CX_DYNAMIC_CHEC
CX_AMDP_ERROR
REPORT zamdp_cust_classification.
* AMDP BAdI Handle The methods are prefixed with CX_AMDP, indicating these classes are exception classes
GET BADI gref_flights_check. for handling errors during AMDP calls. These exception classes belong to the
CX_DYNAM IC_CHECK category and must be declared explicitly using the RAISING
* AMDP BAdI Call addition in the definition of an AMDP method and handled when called in the
CALL BADI gref_flights_check->reclassify application program.
EXPORTING
iv_client = sy-mandt
199
198
Contents
Acknowledgments.....................................................................................................17
1.1 Features................................................................................................. 19
1.1.1 Main Memory and CPU Innovations.....................................................21
1.1.2 Storage Innovations...........................................................................22
1.1.3 Data Compression..............................................................................23
1.1.4 Data Partitioning................................................................................25
1.2 Architecture............................................................................................ 27
1.3 Platform Capabilities................................................................................ 29
1.4 Use Cases............................................................................................... 30
1.4.1 Side-by-Side Scenario.........................................................................30
1.4.2 Fully Integrated Scenarios.......................................................................32
1.4.3 New Applications................................................................................32
1.5 Summary................................................................................................ 33
2 Code-to-Data Paradigm 35
7
Contents Contents
3 Development Environments 51
5 SQLScript Programming 123
3.1 Evolution of Programming Languages and Development Tools.................... 51 5.1 What Is SQLScript?................................................................................ 123
3.2 SAP HANA Studio.................................................................................... 54 5.1.1 SQL versus SQLScript........................................................................124
3.2.1 Compatibility Checks...........................................................................56 5.1.2 Types of SQLScript Statements...............................................................125
3.2.2 Download..........................................................................................60 5.1.3 Prerequisites....................................................................................125
3.2.3 Installation.........................................................................................64
5.2 SQL Query Template.............................................................................. 128
3.2.4 Basic Navigation and Actions...............................................................67
5.2.1 Creating a Schema............................................................................128
3.2.5 ABAP Perspective...............................................................................72
5.2.2 Creating Tables................................................................................129
3.2.6 Modeler Perspective............................................................................75
3.2.7 Administration Perspective..................................................................83 5.3 Stored Procedures................................................................................. 131
5.3.1 Creating Procedures..........................................................................131
3.3 SAP HANA Client..................................................................................... 87
5.3.2 Deleting Procedures..........................................................................133
3.4 ABAP Development Tools......................................................................... 88
5.4 User-Defined Functions.......................................................................... 133
3.4.1 Overview...........................................................................................88
5.4.1 Joins and GROUP BY Clauses.................................................................135
3.4.2 Installation.........................................................................................90
5.4.2 Subqueries.......................................................................................137
3.5 SAP Business Technology Platform, ABAP Environment............................... 99
5.5 Constructs............................................................................................. 139
3.6 Summary.............................................................................................. 101 5.5.1 Local Scalar Variables.......................................................................139
5.5.2 Local Table Variables........................................................................139
5.5.3 Conditional Statements.....................................................................140
5.5.4 FOR Loops and WHILE Loops.................................................................141
4 Native SQL 103
5.5.5 Operators.........................................................................................141
5.5.6 Emptiness Check for Tables and Table Variables......................................143
4.1 Executing Native SQL Statements............................................................ 104 5.5.7 Getting the Number of Records for Tables and Table Variables............144
4.1.1 Literals and Host Variables................................................................105
5.6 Cursors................................................................................................. 144
4.1.2 Statement for Cursor Processing.............................................................105
5.6.1 Example Cursor................................................................................145
4.1.3 Database Procedure Calls.......................................................................106
5.6.2 Looping over Cursors........................................................................146
4.1.4 Statements for Establishing Database Connections.............................107
4.1.5 Data Type Compatibility....................................................................108 5.7 Transactional Statements....................................................................... 147
4.2 ABAP Database Connectivity................................................................... 109 5.8 Dynamic SQL......................................................................................... 148
4.2.1 Querying the Database.....................................................................110 5.9 Exception Handing................................................................................. 149
4.2.2 DDL and DML Operations..................................................................115 5.9.1 Continue after Handling....................................................................149
4.2.3 Secondary Connections..........................................................................118 5.9.2 Block Parallel Execution.....................................................................150
4.2.4 Precautions While Using ABAP Database Connectivity.........................120
5.10 Arrays................................................................................................... 150
4.3 Summary.............................................................................................. 121 5.10.1 Creating an Array.............................................................................150
5.10.2 Accessing the Array..........................................................................151
5.10.3 Concatenating Arrays........................................................................151
5.10.4 Converting a Table into an Array.......................................................151
5.10.5 Unpacking an Array into a Table........................................................152
8 9
Contents Contents
10 11
Contents Contents
14 15
Index
639
Index Index
640 641
Index Index
642 643
Index Index