点击此处---> 群内免费提供SAP练习系统(在群公告中)
加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)
在这篇文章中,您将了解内联数据声明。内联数据声明可从 ABAP 7.40 版获得。
内联声明数据意味着,在语句中声明数据变量、表、字段符号等,将它们用作操作数。
在下面的例子中,变量 lv_number_of_authors 被首先声明,然后在下一条语句中使用。
DATA lv_number_of_authors TYPE i. lv_number_of_authors = 2.
取而代之的是,您可以编写如下。这是内联数据声明。
DATA(lv_number_of_authors) = 2.
在 SAP 中,操作数在很多地方使用,并且几乎所有这些地方都可以进行内联声明。这基本上是允许编译器决定类型,而不是程序员显式定义变量的类型。
所以,在不写太多的情况下,我只会给你留下内联数据声明的例子。
语境 | 显式数据声明 | 内联数据声明 |
变量 | DATA lv_number_of_authors TYPE i. | DATA(lv_number_of_authors) = 2. |
lv_number_of_authors = 2. | ||
变量 | DATA lv_var2. | DATA(lv_var2) = lv_var1. |
lv_var2 = lv_var1. | ||
循环 | DATA ls_work_area LIKE LINE OF lt_itab. | LOOP AT lt_itab INTO DATA(ls_work_area). |
在 | LOOP AT lt_itab INTO ls_work_area. | "Processing steps |
内部 | "Processing steps | ENDLOOP. |
表 | ENDLOOP. | |
循环 | FIELD-SYMBOLS <fs_row> LIKE LINE OF lt_itab. | LOOP AT lt_itab ASSIGNING FIELD-SYMBOL(<fs_row>). |
在 | LOOP AT lt_itab ASSIGNING <fs_row>. | "Processing steps |
内部 | "Processing steps | ENDLOOP. |
表 | ENDLOOP. | |
临时 | DATA: lt_tmp_table LIKE lt_itab. | DATA(lt_tmp_table) = lt_itab. |
内部 | lt_tmp_table = lt_itab. | |
表 | ||
读取 | DATA ls_work_area LIKE LINE OF lt_itab. | READ TABLE lt_itab INTO DATA(ls_work_area) INDEX 1. |
内 | READ TABLE lt_itab INTO ls_work_area INDEX 1. | IF sy-subrc EQ 0. |
表 | IF sy-subrc EQ 0. | "Code for successful READ |
"Code for successful READ | ELSE. | |
ELSE. | "Code for failed READ | |
"Code for failed READ | ENDIF. | |
ENDIF. | ||
读取 | FIELD-SYMBOLS <fs_row> like LINE OF lt_itab. | READ TABLE lt_itab ASSIGNING FIELD-SYMBOL(<fs_row>) INDEX 1. |
内 | READ TABLE lt_itab ASSIGNING <fs_row> INDEX 1. | IF sy-subrc EQ 0. |
表 | IF sy-subrc EQ 0. | "Code for successful READ |
"Code for successful READ | ELSE. | |
ELSE. | "Code for failed READ | |
"Code for failed READ | ENDIF. | |
ENDIF. | ||
读取 | Note: | TRY. |
内 | Old READ statement is used for demonstration. | DATA(ls_work_area1) = lt_itab[ 1 ]. |
表 | New READ syntax can be used as shown here. | "Code for successful READ |
CATCH cx_sy_itab_line_not_found. | ||
"Code for failed READ | ||
ENDTRY. | ||
选择 | TYPES: | SELECT carrid, connid, fldate |
查询 | BEGIN OF ty_fls, | FROM sflight |
carrid TYPE sflight-carrid, | INTO TABLE @DATA(lt_flights). | |
connid TYPE sflight-connid, | ||
fldate TYPE sflight-fldate, | ||
END OF ty_fls. | ||
DATA lt_flights TYPE STANDARD TABLE OF ty_fls. | ||
SELECT carrid connid fldate | ||
FROM sflight | ||
INTO TABLE lt_flights. | ||
方法 | DATA lv_result TYPE char5. | sample_method( |
调用 | sample_method( | EXPORTING iv_input = iv_test |
EXPORTING iv_input = iv_test | IMPORTING iv_result = DATA(lv_result) ). | |
IMPORTING iv_result = lv_result ). | ||
支持 | DATA lv_count TYPE i. | FIND 'J' IN 'JAGDISH' MATCH COUNT DATA(lv_count). |
变量 | FIND 'J' IN 'JAGDISH' MATCH COUNT lv_count. | |
对象 | DATA lo_obj TYPE REF TO lcl_abap. | DATA(lo_obj) = NEW lcl_abap( ). |
CREATE OBJECT lo_obj. | ||
工厂 | DATA : | TRY. |
方法 | lo_table TYPE REF TO cl_salv_table, | cl_salv_table=>factory( |
调用、 | lo_function TYPE REF TO cl_salv_functions_list, | IMPORTING r_salv_table = DATA(lo_table) |
对象 | lx_msg TYPE REF TO cx_salv_msg, | CHANGING t_table = lt_output_tab ). |
和 | lx_alv TYPE REF TO cx_salv_not_found. | DATA(lo_function) = lo_table->get_functions( ). |
异常 | lo_function->set_all( ). | |
TRY. | CATCH cx_salv_msg INTO DATA(lx_msg). | |
cl_salv_table=>factory( | "Handle Error | |
IMPORTING r_salv_table = lo_table | CATCH cx_salv_not_found INTO DATA(lx_alv). | |
CHANGING t_table = lt_output_tab). | "Handle Error | |
ENDTRY. | ||
lo_function = lo_table->get_functions( ). | ||
lo_function->set_all( ). | ||
CATCH cx_salv_msg INTO lx_msg. | ||
"Handle Error | ||
CATCH cx_salv_not_found INTO lx_alv. | ||
"Handle Error | ||
ENDTRY. |
使用这种方法声明数据减少了代码行数,在结构中添加字段等更改很容易实现,它为程序员提供了灵活性,并消除了使用太多辅助变量的需要。