What’s in this document

This document explains the different usage patterns of SELECT-OPTIONS while consuming SAP HANA artifacts from within ABAP.

Introduction

      ABAP reports generally have SELECT-OPTIONS to enable end users to provide the selection inputs. This input is later used in the implementation of the ABAP business logic as per the needs of the end user to:

  • restrict data fetched from the database.
  • process data fetched from the database.

     In the context of ABAP on HANA, to fully utilize SAP HANA capabilities, there could be use-cases where the data fetching and data processing logic of the report has been pushed down to the SAP HANA layer to accelerate the ABAP report.

     This document outlines some of the means by which the SELECT-OPTIONS can be used while consuming SAP HANA artifacts from within ABAP code with simple examples.

Data Model

    This document contains sample scenario of Open Items Analysis to illustrate the different means of using SELECT-OPTIONS to consume HANA artifacts. Consider an ABAP report fetching the list of closed invoices for business partners and the possibility to fetch the list of closed invoices only for specific business partners chosen from the selection screen of the report.

     The sample scenario uses the ABAP Dictionary View, SEPMAPPS_CLSDINV – an external view created for HANA Calculation view, CA_CLOSED_INVOICES. You can find this Calculation view and the corresponding Dictionary View in all SAP NetWeaver AS ABAP 7.4 SP5 systems.

Note: From SAP NetWeaver AS ABAP 7.4 SP2, you can create ABAP Dictionary representation in ABAP for SAP HANA views. For instance,  SEPMAPPS_CLSDINV is the Dictionary view of underlying SAP HANA Calculation View CA_CLOSED_INVOICES, which is based on the open item analysis scenario. The SAP HANA Calculation view CA_CLOSED_INVOICES fetches all the closed invoices of business partners.

SELECT-OPTIONS

     In ABAP, it is a common practice to use SELECT-OPTIONS while creating selection screens. The statements SELECT-OPTIONS and PARAMETERS determine the technical interface and the user interface. The parameters and select-options you specify are displayed on the selection screen for the end users to enter values.

     Now, define one selection screen using the statement SELECT-OPTIONS [bupa_id], and then use this in all the further scenarios. In order to define the selection screen, use the dictionary view SEPMAPPS_CLSDINV.

DATA ls_sepmapps_clsdinv TYPE SEPMAPPS_CLSDINV.

DATA lt_sepmapps_clsdinv TYPE table of SEPMAPPS_CLSDINV.

SELECT-OPTIONS bupa_id FOR ls_sepmapps_clsdinv-BUPA_ID.

Options with SELECT-OPTIONS

      The rest of the document will assist you to explore the different options of using SELECT-OPTIONS in SQL queries. The subsequent sections contain examples to explain how to apply SELECT-OPTIONS while consuming the following:

  1. SAP HANA View [as DDIC View] using Open SQL
  2. SAP HANA View using Native SQL [ADBC]
  3. SAP HANA Database Procedure [as DB Procedure Proxy] using Open SQL

1. Usage of SELECT-OPTIONS in Open SQL

     Using SELECT-OPTIONS is simple and straightforward in Open SQL queries. As an example, the query below fetches the closed invoices for one or more business partners defined in the SELECT-OPTIONS, bupa_id.

SELECT * FROM SEPMAPPS_CLSDINV INTO CORRESPONDING FIELDS OF TABLE

lt_sepmapps_clsdinv WHERE BUPA_ID in ( bupa_id ). ” bupa_id is a SELECT-OPTIONS variable

2. SELECT-OPTIONS and SAP HANA View using Native SQL (ADBC)

2.1 Convert SELECT-OPTIONS to WHERE clause

     SELECT-OPTIONS is an ABAP language construct/statement. Hence while using SELECT-OPTIONS in the Native SQL query, it is necessary to transform the data of the SELECT-OPTIONS appropriately. As described earlier, SELECT-OPTIONS is used to define selection criteria – used to filter data
fetched from the database. This means the data of the SELECT-OPTIONS ideally form the WHERE condition of the SQL query.

     As shown in the code snippet below, using the ABAP API, it is possible to construct the WHERE clause string from SELECT-OPTIONS.

DATA(lv_sel_tab) = cl_lib_seltab=>new( it_sel = bupa_id[] ).

DATA(lv_where_clause) = lv_sel_tab->sql_where_condition( iv_field = ‘BUPA_ID’ ).

2.2 Use SELECT-OPTIONS as a WHERE clause in Native SQL using ADBC

     The code snippet below uses the SELECT-OPTIONS in the Native SQL query. The WHERE clause string, that was constructed from the SELECT-OPTIONS [in the previous step – 2.1] is used in the query.

DATA: LO_SQL_STMT TYPE REF TO CL_SQL_STATEMENT,

      LO_CONN     TYPE REF TO CL_SQL_CONNECTION,

      LO_RESULT   TYPE REF TO CL_SQL_RESULT_SET,

      LV_SQL      TYPE STRING,
      LR_DATA     TYPE REF TO DATA.

DATA: LX_SQL_EXC           TYPE REF TO CX_SQL_EXCEPTION,

      LT_SEPMAPPS_CLSDINV  TYPE TABLE OF SEPMAPPS_CLSDINV,

      LV_TEXT              TYPE STRING.

TRY.       

     LV_SQL = | SELECT * |

               && |FROM “_SYS_BIC”.”sap.bc.epm.oia.apps/CA_CLOSED_INVOICES” |

                && |WHERE bupa_id = { LV_WHERE_CLAUSE }  |.

    

     LO_CONN = CL_SQL_CONNECTION=>GET_CONNECTION( ).

     “Create an SQL statement to be executed via the connection

          LO_SQL_STMT = LO_CONN->CREATE_STATEMENT( ).

          “Execute the native SQL query

     LO_RESULT = LO_SQL_STMT->EXECUTE_QUERY( LV_SQL ).

          “Read the result into the internal table lt_sepmapps_clsdinv

          GET REFERENCE OF LT_SEPMAPPS_CLSDINV INTO LR_DATA.

          LO_RESULT->SET_PARAM_TABLE( LR_DATA ).
     LO_RESULT->NEXT_
PACKAGE( ).
     LO_RESULT->
CLOSE( ).


     LO_CONN->
CLOSE( ).


CATCH CX_SQL_EXCEPTION INTO LX_SQL_EXC.

     LV_TEXT = LX_SQL_EXC->GET_
TEXT( ).
     MESSAGE LV_TEXT TYPE ‘E’.

ENDTRY.

3. SELECT-OPTIONS and SAP HANA Database Procedures using Open SQL

Apply_Filter – HANA SQL Function

     From SAP HANA SPS06 onwards it is possible to express the WHERE clause in a dynamic way using the function APPLY_FILTER. APPLY_FILTER consists of two parameters. The first parameter is the data source and the second parameter is the filter condition that needs to be passed as a string value. This filter condition can also be applied on data source such as SAP HANA views and database procedures.

3.1 Convert SELECT-OPTOINS to WHERE clause

     Similar to section 2.1, the code snippet below is used to construct the WHERE clause string from the SELECT-OPTIONS.

DATA(lv_sel_tab) = cl_lib_seltab=>new( it_sel = bupa_id[] ).

DATA(lv_where_clause) = lv_sel_tab->sql_where_condition( iv_field = ‘BUPA_ID’ ).

3.2 Use SELECT-OPTIONS as a WHERE clause in SAP HANA Database Procedure

     The next step is to consume the transformed WHERE condition within the Database Procedure using SQL Script.

  1. Create a Database Procedure, DP_CLOSED_INVOICES, with one scalar input parameter [iv_where_clause] of type string and an output parameter [OUT_PARAM] with the structure similar to output structure of the Calculation view, CA_CLOSED_INVOICES
  2. The logic of the database procedure is to read the closed invoices from the Calculation view, CA_CLOSED_INVOICES and apply a filter [using APPLY_FILTER], which is passed as an input parameter.

DatabaseProc.png

3.3 Create Database Procedure Proxy in ABAP

     In order to consume SAP HANA Database Procedure in ABAP using Open SQL, Database Procedure Proxy must be created in ABAP Dictionary. Create a database procedure proxy, named as ZDP_CLOSED_INVOICES for the procedure created in step 3.2. Refer to the how-to guide for a step-by-step approach on how to create database procedure proxy in ABAP.

3.4 SELECT-OPTIONS as Input Parameter to Database Procedure Proxy

     The code snippet displayed below calls the Database Procedure Proxy from ABAP. In this code snippet, the WHERE condition, is passed to the database procedure as an input parameter.

” ZIF_ZDP_CLOSED_INVOICES is an ABAP interface which holds parameter definitions

DATA : lt_closed_invoices type table of ZIF_ZDP_CLOSED_INVOICES=>out_param.

” ZDP_CLOSED_INVOICES is Database Procedure Proxy

CALL DATABASE PROCEDURE ZDP_CLOSED_INVOICES

     EXPORTING
          IV_WHERE_CLAUSE =
lv_where_clause
     IMPORTING

          OUT_PARAM       =
lt_closed_invoices.

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply