点击此处---> 群内免费提供SAP练习系统(在群公告中)
加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)
在这篇文章中,你将学到
内部表的 For 循环
关键字LET
嵌套 For 循环
单表的 FOR 迭代
FOR 也称为迭代表达式。
代码:
这是表 lt_flights 的简单 For 循环。Index 关键字在这里是可选的。此代码只是将数据从一个表传输到另一个表。
这是编写 LOOP AT 和 APPEND 类代码的另一种方法。
与 LOOP AT 类似,您也可以编写除强制 ( ) 之外的 where 条件。
FOR 迭代和 LET 表达式
LET 可用于定义变量并将它们分配给目标表字段。
嵌套 FOR 迭代
这类似于 LOOP 中的 LOOP。这样可以嵌套多个 FOR 迭代。
Code in text format
TYPES : BEGIN OF ty_flight, seq_num type i, carrier TYPE s_carrname, connect TYPE s_conn_id, fldate TYPE s_date, END OF ty_flight. DATA lt_new_flights TYPE STANDARD TABLE OF ty_flight. SELECT * FROM sflight INTO TABLE @DATA(lt_flights). IF sy-subrc EQ 0. SELECT * FROM scarr INTO TABLE @DATA(lt_scarr). ENDIF. "FOR Iteration lt_new_flights = VALUE #( FOR ls_flight IN lt_flights INDEX INTO lv_index WHERE ( carrid = 'AA' AND connid = '0017' ) LET lv_carrname = lt_scarr[ carrid = ls_flight-carrid ]-carrname IN carrier = lv_carrname ( seq_num = lv_index connect = ls_flight-connid fldate = ls_flight-fldate ) ). cl_demo_output=>display( lt_new_flights ).
"LOOP AT Method DATA: ls_new_flight TYPE ty_flight. LOOP AT lt_flights INTO DATA(ls_flight). ls_new_flight-seq_num = sy-tabix. ls_new_flight-carrier = lt_scarr[ carrid = ls_flight-carrid ]-carrname. ls_new_flight-connect = ls_flight-connid. ls_new_flight-fldate = ls_flight-fldate. APPEND ls_new_flight TO lt_new_flights. ENDLOOP. cl_demo_output=>display( lt_new_flights ).
"Nested FOR Iterations lt_new_flights = VALUE #( FOR ls_scarr in lt_scarr FOR ls_flight IN lt_flights WHERE ( carrid = ls_scarr-carrid ) ( carrier = ls_scarr-carrname connect = ls_flight-connid fldate = ls_flight-fldate ) ).