Get Example source ABAP code based on a different SAP table
LOOP AT itab>, Basic Form Short Reference >
ABAP_SYNTAX LOOP AT itab result > $[cond>$].> ...> BEGIN_SECTION VERSION 5 OUT $[AT ...> ...> ENDAT.>$]> ...> END_SECTION VERSION 5 OUT ENDLOOP.>
What does it do? This variant of the statement LOOP AT itab>> sequentially reads lines of the internal table itab> and executes the statement block between LOOP> and ENDLOOP> once for each line. Either all lines are read or a subset specified by one or more conditions cond>>.
The output behavior result>> determines how and to where the line content is read.
The conditions cond>> can consist of the following optional additions:
The table key with which the loop is executed can be determined with an addition USING KEY>>. The table key affects the order in which the lines are processed.
Using the additions FROM>> and TO>> for index tables and WHERE>> for all table categories, the loop processing can be restricted to a subset of lines.
An addition STEP>> allows a step size and the direction of the loop processing to be defined. BEGIN_SECTION VERSION 5 OUT
The control statements AT ... ENDAT>> can be used to define control structures for group level processing>. END_SECTION VERSION 5 OUT The order in which the lines are read can depend on:
The table category>
ABAP AdditionUSING KEY>>
ABAP AdditionSTEP>> If none of the additions is specified, the standard order depends on the table category as follows:
Standard tables and sorted tables> The lines are read by ascending line numbers in the primary table index>. In each loop pass, the system field sy-tabix> contains the line number of the current line in the primary table index.
Hashed tables> The lines are processed in the order in which they were inserted into the table, and after a sort using the statement SORT> in the sort order. In each loop pass, the system field sy-tabix> contains the value 0. The loop is executed until all the table lines of the table or the subset as specified in the cond >> condition have been read or until it is exited with a statement >. If no corresponding lines are found or if the internal table is empty, the loop is not executed at all. If the internal table is specified as the return value or result of a functional method>, a constructor expression>, or a table expression>, the value is persisted for the duration of the loop. Afterwards, the internal table can no longer be accessed. ABAP_CAUTION Special rules apply> when changing the internal table within a loop. In particular you should never perform write accesses on a complete table body. System Fields> This variant of the statement LOOP AT> sets the value of the system field sy-tabix>:
In each loop pass for index tables> and when using a sorted key> on the line number of the current table line in the associated table index.
In hashed tables> and when using a hash key> on the value 0. LOOP AT> does not modify sy-subrc>. After exiting the loop using ENDLOOP>, sy-tabix> is set to the value that it had before entering the loop and that applies to sy-subrc>: sy-subrc>>Meaning> 0At least one loop pass was executed. 4No loop pass was executed. The system fields sy-tfill> and sy-tleng> are also filled.
Latest notes:
If the internal table itab> is specified using a reference variable>, the loop is executed completely using the table referenced at entry. Any changes to the reference variable do not have an effect on the loop. The associated object cannot be deleted by the Garbage Collector> until the loop has been completed. The same thing applies if the table is represented by a field symbol. After the implementation of the field symbol in the loop, iteration still takes place using the table linked to the field symbol when LOOP> is entered.