SAP ITAB LOOP CHANGE



Get Example source ABAP code based on a different SAP table
  



ABAP_ITAB - Changing Internal Tables in a Loop
ITOC

Inserting and Deleting Lines
In the statement block of a LOOP control statement, the content of the currently processed internal table can be changed by inserting or deleting lines. The position of inserted or deleted lines with regard to the current line is determined by the line numbers in the corresponding table index in the case of loops across index tables or when using a sorted key. In the case of loops on hashed tables and when using a hash key, the position depends on the order of insertion or the order of lines that might have been established by a previous SORT statement for the table.
Inserting lines after the current line causes these new lines to be processed in the subsequent loop passes, which can produce an endless loop.
Deleting lines after the current line causes the deleted lines to no longer be processed in the subsequent loop passes.
Inserting lines in front of the current line causes the internal loop counter to be increased by one with each inserted line. This affects sy-tabix in the subsequent loop pass in the case of loops across index tables or when using a sorted key, and sy-tabix is increased accordingly.
Deleting the current line or lines in front of the current line causes the internal loop counter to be decreased by one with each deleted line. In the case of loops across index tables or when using a sorted key, this affects sy-tabix in the subsequent loop pass, and sy-tabix is decreased accordingly.

Replacing or Clearing the Entire Table Body
As a rule the replacement or clearing of the entire table body of the currently processed internal table is not allowed in the statement block of a LOOP statement. Such a write access to the table body can be achieved with statements such as CLEAR, FREE , LOCAL,
BEGIN_SECTION VERSION 5 OUT REFRESH,
END_SECTION VERSION 5 OUT SORT, DELETE ... WHERE, and with all types of assignments.
If known statically, clearing or replacing the table body inside a loop results in a syntax error in classes and for LOOP statements with a statically known secondary key. Outside classes and for tables without secondary key, the syntax check gives only a warning for compatibility reasons.
If it is not known statically, clearing or replacing the table body inside a loop raises the runtime error TABLE_FREE_IN_LOOP with the following exception: If the output behavior is defined with INTO or TRANSPORTING NO FIELDS , or with the obsolete short form, for compatibility reasons there is no runtime error and the loop is exited in an undefined way.
BEGIN_SECTION SAP_INTERNAL_HINT
In many cases, the loop is exited at the next loop pass and statements behind clearing or replacing the table body are still executed in the current loop pass. But if the table body is replaced using a (constructor) expression, the loop continues on the changed table.
END_SECTION SAP_INTERNAL_HINT
ABAP_CAUTION Since the result of replacing or clearing the entire table body within a loop can result in unpredictable program behavior, the exceptions to the rule should not be exploited.

ABAP_PGL
Loop Processing
ABAP_PGL_END

ABAP_EXAMPLE_VX5
Replacing the table body of an internal table within a loop over that table. The syntax check reports errors in classes and warnings outside classes. While the loop with ASSIGNING results in the runtime error TABLE_FREE_IN_LOOP, the loop with INTO is executed without runtime error. The latter is shown for demonstration purposes, but not recommended.
ABEXA 01606
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
Deleting all lines of an internal table within a loop over that table. while the loop with ASSIGNING results in runtime error TABLE_FREE_IN_LOOP the loop with INTO is executed without runtime error. The latter is shown for demonstration purposes, but not recommended. Since the deletion happens in a method, it is not detected by the syntax check.
ABEXA 01598
ABAP_EXAMPLE_END