如何在AMDP中使用SELECT-OPTION

转载自:http://www.sapyard.com/abap-on-sap-hana-part-x-amdp-with-select-options/


SAP ABAP for HANA

In the previous post on AMDP in SAP HANA , we learned about the basics of AMDP. What is AMDP, How to declare an AMDP class, How to identify an AMDP method? We also consumed the AMDP Class method is our Program and displayed the output.

If you have not visited the earlier article or if you have not worked in AMDP earlier, we would sincerely suggest you have a glance of that topic so that you can understand this and appreciate this article.

In the example demonstrated in the earlier article, all the selection screen elements were PARAMETERS. Using the PARAMETERS in AMDP Method SELECTs were straight forward. Today we would show how we can pass SELECT OPTIONs of the screen to AMDP Methods and use them. Please note, we cannot directly pass SELECT options as is it to AMDP Methods. This is one limitation of AMDP. We need to select the data from the database and then APPLY the Filter using the function APPLY_FILTER.

Let us hit it hard again. AMDP Class-Methods cannot take SELECT OPTIONS as input. So SELECT OPTIONS need to be converted to FILTER STRING using some way and then pass the FILTER STRING as an input PARAMETER of the of the AMDP Method.

The actual syntax to filter the selected data would look like below:

* Filtration based on Selection screen input
ex_it_tcode_role = APPLY_FILTER( :ex_it_tcode_role, :ip_filters );

EX_IT_TCODE_ROLE would have all the data and APPLY_FILTER would keep the subset using IP_FILTERS value.

How do we pass IP_FILTERS?
Ans: It has to be passed as STRING.

METHODS get_t_code_role_matrix
IMPORTING
VALUE(ip_object) TYPE agobject
VALUE(ip_langu) TYPE menu_spras
VALUE(ip_line) TYPE menu_num_5
VALUE(ip_filters) TYPE string " PARAMETER for the SELECT OPTION String
EXPORTING
VALUE(ex_it_tcode_role) TYPE tt_tcode.

How do we generate the filter string from SELECT OPTIONS?
Ans: You are the programmer, you find your way to generating the filter. 

It should act as the WHERE clause. Or like the FILTER using RANGE table. 

Do not worry, we would show you an easy way. 

Handle SELECT OPTION in AMDP

If S_TCODE and S_ROLE are two SELECT OPTIONS of a program, then the string for AMDP filter can be generated using the class CL_SHDB_SELTAB method COMBINE_SELTABS as shown below.

abap-on-hana-amdp

DATA(lv_where) = cl_shdb_seltab=>combine_seltabs(
it_named_seltabs = VALUE #(
( name = 'TCODE' dref = REF #( s_tcode[] ) )
( name = 'ROLE' dref = REF #( s_role[] ) )
) ).

If the above syntax is little confusing, then check the alternative for the same syntax.

cl_shdb_seltab=>combine_seltabs(
EXPORTING
it_named_seltabs = VALUE #(
( name = 'TCODE' dref = REF #( s_tcode[] ) )
( name = 'ROLE' dref = REF #( s_role[] ) )
)
RECEIVING
rv_where = DATA(lv_where) ).

Feeling better now?

Add class CL_SHDB_SELTAB method COMBINE_SELTABS on your cheat sheet. 

Frequently Asked Question on HANASAP HANA for Beginners from a Beginner?

What does the above class method do?
Ans: See it yourself in debug mode. 

HOW TO USE SELECT-OPTION IN AMDP

I am sure by now you are curious to know how we use it in the Program (after all you are a programmer by heart). 

Real Time working Program to show handling of SELECT OPTION in AMDP:

*--------------------------------------------------------------------*
* Created by: www.sapyard.com
* Created on: 29th Nov 2016
* Description: This program consumes the AMDP Class/Method and
* shows how to send SELECT OPTIONS to AMDP and use
* APPLY_FILTER function in AMDP Method.
*--------------------------------------------------------------------*
REPORT zmm_tcode_role_report NO STANDARD PAGE HEADING
LINE-COUNT 132.
*--------------------------------------------------------------------*
* TABLES
*--------------------------------------------------------------------*
TABLES: agr_define.
 
*--------------------------------------------------------------------*
* DATA DECLARATION
*--------------------------------------------------------------------*
* Inline data declaration for the AMDP Class Instance
DATA(lr_data) = NEW zcl_user_role_amdp( ).
 
*--------------------------------------------------------------------*
* SELECTION SCREEN
*--------------------------------------------------------------------*
SELECTION-SCREEN: BEGIN OF BLOCK block1 WITH FRAME TITLE text-t01.
SELECT-OPTIONS:
s_tcode FOR syst-tcode,
s_role FOR agr_define-agr_name.
SELECTION-SCREEN: END OF BLOCK block1.
 
*--------------------------------------------------------------------*
* INITIALIZATION.
*--------------------------------------------------------------------*
 
*--------------------------------------------------------------------*
* START-OF-SELECTION.
*--------------------------------------------------------------------*
START-OF-SELECTION.
 
* Build where clause for data fetching
* Class-Method to convert the select options to a dynamic where clause which
* will be passed to the AMDP for data filteration after data selection
DATA(lv_where) = cl_shdb_seltab=>combine_seltabs(
it_named_seltabs = VALUE #(
( name = 'TCODE' dref = REF #( s_tcode[] ) )
( name = 'ROLE' dref = REF #( s_role[] ) )
) ).
 
* Calling the AMDP method to get the data
CALL METHOD lr_data->get_t_code_role_matrix
EXPORTING
ip_object = 'S_TCODE'
ip_langu = sy-langu
ip_line = '00000'
ip_filters = lv_where
IMPORTING
ex_it_tcode_role = DATA(it_tcode_role).
 
*--------------------------------------------------------------------*
* END-OF-SELECTION.
*--------------------------------------------------------------------*
END-OF-SELECTION.
 
* Publishing the data in an output
cl_demo_output=>display_data(
EXPORTING
value = it_tcode_role
name = 'AMDP to show APPLY_FILTER function' ).
*--------------------------------------------------------------------*
Real AMDP Class Method showing usage of APPLY_FILTER for SELECT OPTIONS:

CLASS zcl_user_role_amdp DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
 
PUBLIC SECTION.
 
INTERFACES if_amdp_marker_hdb.
 
TYPES:
BEGIN OF ty_tcode,
tcode TYPE agval,
ttext TYPE ttext_stct,
role TYPE agr_name,
rtext TYPE agr_title,
END OF ty_tcode .
 
TYPES:
tt_tcode TYPE STANDARD TABLE OF ty_tcode .
 
METHODS get_t_code_role_matrix
IMPORTING
VALUE(ip_object) TYPE agobject
VALUE(ip_langu) TYPE menu_spras
VALUE(ip_line) TYPE menu_num_5
VALUE(ip_filters) TYPE string
EXPORTING
VALUE(ex_it_tcode_role) TYPE tt_tcode.
 
PROTECTED SECTION.
PRIVATE SECTION.
 
ENDCLASS.
 
CLASS zcl_user_role_amdp IMPLEMENTATION.
 
METHOD get_t_code_role_matrix
BY DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING agr_1251 tstct agr_texts.
 
ex_it_tcode_role = select a.low,
b.ttext,
a.agr_name,
c.text
from agr_1251 as a
inner join tstct as b on a.low = b.tcode
inner join agr_texts as c on a.agr_name = c.agr_name
where
a.mandt = :ip_client
AND a.object = :ip_object
AND b.sprsl = :ip_langu
AND c.spras = :ip_langu
AND C.LINE = :ip_line
ORDER BY a.low, a.agr_name;
 
* Filtration based on Selection screen input
ex_it_tcode_role = APPLY_FILTER( :ex_it_tcode_role, :ip_filters );
 
ENDMETHOD.
 
ENDCLASS.







  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值