ABAP随笔4- 新语法 for in / for then until

2022-06-23 15:11:47 浏览数 (1)

废话不多,先放上for in 和 for then until

代码语言:javascript复制
"今天学习一下FOR的语法,常用的2个语法。
"FOR wa|<fs> IN itab [INDEX INTO idx] [cond]

"FOR i = … [THEN expr] UNTIL|WHILE log_exp

DATA gs_mara TYPE mara.
SELECT * FROM mara INTO TABLE @DATA(gt_mara) UP TO 10 ROWS.

*FOR wa|<fs> IN itab [INDEX INTO idx] [cond]
TYPES:ty_matnr   TYPE mara-matnr,
      ty_t_matnr TYPE TABLE OF ty_matnr WITH EMPTY KEY.


"1 将gt_mara 赋值给 gt_maktx
*旧语法:
DATA gt_matnr1 TYPE ty_t_matnr.
DATA gs_matnr1 TYPE ty_matnr.
LOOP AT gt_mara INTO gs_mara WHERE mtart = 'ROH'.
  .
  gs_matnr1 = gs_mara-matnr.
  APPEND gs_matnr1 TO gt_matnr1.
ENDLOOP.
*新语法 ls_mara仅为临时变量,程序会临时定义并赋值,ls_matnr-matnr 和 ty_t_matnr的行类型一定要保持一致
"功能是将 gt_mara中的物料号赋值给gt_matnr2
DATA(gt_matnr2) = VALUE ty_t_matnr( FOR ls_mara IN gt_mara ( ls_mara-matnr ) ).

*当加上where条件时
DATA(gt_matnr3) = VALUE ty_t_matnr( FOR ls_mara IN gt_mara WHERE ( mtart = 'ROH' ) ( ls_mara-matnr ) ).


"for with THEN and UNTIL | WHERE

*旧语法:
TYPES:BEGIN OF ty_line,
        a TYPE i,
        b TYPE i,
        c TYPE i,
      END OF ty_line,
      ty_t_line TYPE TABLE OF ty_line WITH EMPTY KEY.
DATA j TYPE i.
DATA gt_lines1 TYPE ty_t_line.
FIELD-SYMBOLS <ls_line1> TYPE ty_line.
j = 0.

DO.
  j = j   1.
  IF j > 10. EXIT. ENDIF.
  APPEND INITIAL LINE TO gt_lines1 ASSIGNING <ls_line1>.
  <ls_line1>-a = j.
  <ls_line1>-b = j   1.
  <ls_line1>-c = j   2.
ENDDO.

*新语法 x未表达式中隐式定义的变量
DATA(gt_lines2) = VALUE ty_t_line( FOR x = 1 THEN x   1 UNTIL x > 10
( a = x b = x   1 c = x   2 ) ).

-TAB 热爱技术 享受生活

0 人点赞