LOOP AT itab result [cond].
...
ENDLOOP.
The LOOP and ENDLOOP statements define a loop around a statement block. The LOOP statement reads lines from internal table itab sequentially. You can either read all the lines or specify conditions cond to restrict which lines to read. The output result determines when and where the line contents are read. The statement block between LOOP and ENDLOOP is executed once for each line. To exit processing of the statement block , you can use the statements described in Section leave loops.
The sequence in which the lines are read depends on the table type:
The loop continues to run until all the table lines that meet conditition
cond have been read or until it is exited with a statement. If no appropriate lines are found or if the internal table is blank, the loop is not run at all.
System Fields
The LOOP ATstatement sets system field sy-tabix to the table index of the current table line for Standard tables and sorted Tables, and to 0 for Hashed-Tables, after every loop pass. It leaves sy-subrc unchanged. When the loop is exited with ENDLOOP, sy-tabix is reset to the value it had before the loop was entered, and the following applies sy-subrc:sy-subrc | Relevance |
0 | The loop was run at least once. |
4 | The loop was not run at all. |
The system fields sy-tfill and sy-tleng are also supplied with data.
Changing Internal tables in a loop
If you insert or delete lines in the statement block of a LOOP , this will have the following effects:
Nested LOOP-loops. The contents of the current row for the outer loop are analyzed in the WHERE-condition for the inner loop.
PARAMETERS p_name TYPE scarr-carrname DEFAULT '*'.
DATA: scarr_tab TYPE SORTED TABLE OF scarr
WITH UNIQUE KEY carrname,
spfli_tab TYPE SORTED TABLE OF spfli
WITH NON-UNIQUE KEY carrid.
FIELD-SYMBOLS
DATA spfli_line LIKE LINE OF spfli_tab.
SELECT *
FROM scarr
INTO TABLE scarr_tab.
SELECT *
FROM spfli
INTO TABLE spfli_tab.
LOOP AT scarr_tab ASSIGNING
WHERE carrname CP p_name.
LOOP AT spfli_tab INTO spfli_line
WHERE carrid =
WRITE: / spfli_line-carrid,
spfli_line-connid.
ENDLOOP.
ENDLOOP.
The following loop deletes all lines of an internal table since - through the short form of the DELETE statement - the current first line is always deleted.
DATA itab TYPE TABLE OF i.
DATA wa LIKE LINE OF itab.
LOOP AT itab INTO wa TO 6.
DELETE itab.
ENDLOOP.
Non-Catchable Exceptions