单个表使用 FOR 迭代
FOR
语句也称为迭代表达式。
你可以使用以下代码:
*&---------------------------------------------------------------------*
*& Report ZDEMO_FOR_LOOP
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdemo_for_loop.
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 = 0.
SELECT * FROM scarr INTO TABLE @DATA(lt_scarr).
lt_new_flights = VALUE #(
FOR ls_flight IN lt_flights INDEX INTO lv_index
(
seq_num = lv_index
carrier = lt_scarr[ carrid = ls_flight-carrid ]-carrname
connect = ls_flight-connid
fldate = ls_flight-fldate
)
).
cl_demo_output=>display( lt_new_flights ).
ENDIF.
这是一个针对表 lt_flights
的简单 For 循环。索引关键字在这里是可选的。这段代码只是将数据从一个表转移到另一个表。
这是另一种编写 LOOP AT
和 APPEND
类型代码的方法。
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_flgihts
ENDLOOP.
标签:flight,内表,内表到,flights,语法,lt,ls,new,TYPE
From: https://blog.csdn.net/SAP_yu/article/details/144540951