Ansicht
Dokumentation
ABAPLOOP_AT_ITAB - LOOP AT ITAB
BAL Application Log Documentation SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3upThis documentation is copyright by SAP AG.
LOOP AT itab, Basic Form
LOOP AT itab result $[cond$].
...
$[AT ...
...
ENDAT.$]
...
ENDLOOP.
Effect
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.
- The control statements AT ... ENDAT can be used to define control structures for group level processing.
The order in which the lines are read can depend on:
- The table category
- The addition USING KEY
- The addition STEP
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 additions 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.
For changing the internal table within a loop, special rules apply. Especially, you should never do a write access to the 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 |
0 | The loop was run at least once. |
4 | The loop was not run. |
The system fields sy-tfill and sy-tleng are also filled.
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.
-
There is no implicit selection of a suitable key or index. The used table key or table index is always specified uniquely. The syntax check issues a warning if there is a suitable
secondary
table key but this table key is not used. This warning should be removed through using the key. However, in exceptional cases, it can be bypassed using a
pragma.
- It is generally better to read multiple lines in a LOOP than making multiple individual line reads using the statement READ TABLE or table expressions.
- For compatibility reasons, when a table body is replaced in the loop, a runtime error only occurs when a directly specified table is read without a specified secondary key and when a work area wa is specified for the output behavior result.
- Using a special variant LOOP AT mesh_path, a loop can be executed across the last node of a mesh path.
- Another form of table iterations is possible using
iteration
expressions and FOR in certain constructor expressions.
Example
Loop across an internal table constructed using the
value operator
VALUE, where each
line is assigned to a field symbol declared inline using FIELD-SYMBOL.
Example
Nested LOOP loops without explicit key declaration. The content of the current line for the outer loop is evaluated in the WHERE condition for the inner loop.
Exceptions
Catchable Exceptions
- Cause: Error in a dynamic WHERE condition
Runtime Error: DYN_WHERE_PARSE_ERROR
Non-Catchable Exceptions
- Cause: Illegal parsing of the LOOP field symbol in the body of the loop.
Runtime Error: ITAB_ILLEGAL_REG
- Cause: Illegal assignment to the LOOP reference in the body of the loop.
Runtime Error: MOVE_TO_LOOP_REF
- Cause: Invalid change of entire table body in the loop
Runtime Error: TABLE_FREE_IN_LOOP
- Cause: Invalid access to table index when using a hash key.
Runtime Error: ITAB_ILLEGAL_INDEX_OP
CPI1466 during Backup rdisp/max_wprun_time - Maximum work process run time
This documentation is copyright by SAP AG.
Length: 11449 Date: 20240427 Time: 005234 sap01-206 ( 161 ms )