ARTICLE
Inserting and/or Deleting Table Lines Within LOOP ... ENDLOOP
If you are processing an internal table itab with
LOOP ... ENDLOOP and you
insert new lines in the table
delete existing lines from the table
these changes automatically become effective on the next loop pass.
To understand how insert/delete operations in loop processing work,
imagine an internal table as a concatenated list of table lines:
When the system performs insert operations after the current
table entry (performed with INSERT ,
APPEND , or COLLECT ), newly
inserted lines are processed in subsequent loop passes.
If, for example, you are in the 47th loop pass and insert a new line
before line 48, the next (that is 48th) loop pass will first process
the newly inserted line as line 48 and leave processing of the old line
48 (that is the line before which you inserted the new line) until the
next (that is 49th) loop pass when it becomes line 49.
In the next loop pass, SY-TABIX is set to 48 and then to 49.
When inserting lines after the current table entry, you should beware
of the risk of programming an endless loop.
When the system performs delete operations after the current
table entry (performed with DELETE ), the
deleted lines are not processed in subsequent loop passes.
If, for example, you are in the 47th loop pass and delete line 48, the
next (that is 48th) loop pass will process the old line 49 (that is the
line following the deleted line) as the new line 48.
In the next loop pass, SY-TABIX is set accordingly to 48.
When the system performs insert operations before or on the
current table entry (performed with INSERT ),
the internal loop counter is incremented accordingly.
If, for example, you are in the 47th loop pass and insert a new line
before lines 17, 24 and 33, the next (that is 48th) loop pass will
process the old line 48 (that is line 48 before the three insert
operations) as the new line 51.
In the next loop pass, SY-TABIX is set to 51.
In the case of delete operations before or on the current table
entry (performed with DELETE ), the internal
loop counter value is reduced accordingly.
If, for example, you are in the 47th loop pass and delete the lines 17,
24 and 33, the next (that is 48th) loop pass will process the old line
48 (that is line 48 before the three delete operations) as the new line
45.
In the next loop pass, SY-TABIX is set to 45.
The following construction in particular causes the deletion of the
whole internal table, because, when you use the DELETE
statement, it is always the current first line that is deleted:
LOOP AT itab INTO wa FROM n TO m.
DELETE itab.
ENDLOOP.
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved