今天学习一下内表的READ TABLE 表达式在ABAP 7.4之后的语法:
SELECT * FROM mara INTO TABLE @DATA(gt_mara)
UP TO 10 ROWS.
DATA gt_mara_sort TYPE SORTED TABLE OF mara WITH UNIQUE KEY primary_key COMPONENTS matnr.
"旧语法
DATA LV_MATKL TYPE MARA-MATKL.
READ TABLE GT_MARA INDEX 1 INTO DATA(LS_MARA).
READ TABLE gt_mara_sort INTO DATA(ls_mara_sort1) INDEX 1 USING KEY primary_key.
READ TABLE gt_mara_sort INTO DATA(ls_mara_sort2) WITH KEY matnr = 'AT001'.
IF SY-SUBRC EQ 0.
LV_MATKL = LS_MARA_SORT2-MATKL.
ENDIF.
"新语法
DATA(LS_MARA_NEW) = GT_MARA[ 1 ].
DATA(ls_mara_sort1_new) = gt_mara_sort1[ KEY primary_key INDEX 1 ].
"若是gt_mara_sort已经按照matnr排序,则按照二分法搜索
DATA(ls_mara_sort2_new) = gt_mara_sort2[ matnr = 'AT001' ].
DATA(LV_MATKL_NEW) =
GT_MARA_SORT2[ MATNR = 'AT001' ]-MATKL.
"检查是否存在某个数据
"旧语法
READ TABLE gt_mara TRANSPORTING NO FIELDS WITH KEY matnr = 'AT001'.
"新语法
CHECK line_exists( gt_mara[ matnr = 'AT001' ] ).
"得到数据在内表中的行
"旧语法
DATA lv_tabix TYPE sy-tabix.
READ TABLE gt_mara TRANSPORTING NO FIELDS WITH KEY matnr = 'AT001'.
IF sy-subrc EQ 0.
lv_tabix = sy-tabix.
ENDIF.
"新语法
DATA(lv_tabix_new) = line_index( gt_mara[ matnr = 'AT001' ] ).
注意事项:若当GT_MARA 中不存在 matnr = 'AT001'的数据时, 直接执行
DATA(LS_MARA) = GT_MARA[ MATNR = 'AT001' ]. 会使程序dump,所以一般写成如下的样子:
代码语言:javascript复制ASSIGN gt_mara[ matnr = 'AT001' ] TO FIELD-SYMBOL(<fs>).
IF sy-subrc EQ 0.
* ...
ENDIF.
- TAB 学习技术 热爱生活