ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → LOOP AT itab
LOOP AT itab - cond
Other versions: 7.31 | 7.40 | 7.54
Syntax
... [USING KEY keyname] [FROM idx1] [TO idx2] [WHERE log_exp|(cond_syntax)] ... .
Extras
1. ... USING KEY keyname
2. ... [FROM idx1] [TO idx2]
3. ... WHERE log_exp
4. ... WHERE (cond_syntax)
Effect
USING KEY keyname
is used to determine
the table key with which the loop is executed. The table rows to be read in a LOOP
loop can also be limited using optional conditions. If no conditions are declared, all table rows are read.
Within the loop, the key being used can be addressed using the predefined loop_key
.
This is possible in all statements where the table key
keyname is used and where it can be declared explicitly. This type of statement must then
be executed in the loop itself. Including the statement in a procedure that is called in the loop is not sufficient.
Addition 1
... USING KEY keyname
Effect
The USING KEY
addition can be used to specify a table key
keyname
with which the processing is carried out. The specified table key influences the sequence in which the table rows are accessed, and the evaluation of the remaining conditions.
If the primary table
key is specified using the name primary_key
, the processing behaves in the same way as when no key is explicitly specified. If a
secondary table key is specified, the sequence in which the rows are accessed is as follows:
-
Specification of a sorted key
The rows are processed by ascending row number in the secondary table index In each loop pass, the system fieldsy-tabix
contains the row number of the current row in the associated secondary table index. -
Specification of a hash key
The rows are processed in the sequence in which they were inserted into the table. In each loop pass, the system fieldsy-tabix
contains the value 0.
Notes
-
Unlike the processing of a hash table when a primary key is used, a preceding sort using the
SORT
statement has no influence on the processing sequence when a secondary hash key is specified. -
If a secondary table key is specified, any
WHERE
condition also specified must be optimizable. Otherwise a syntax error occurs or an exception is raised.
Addition 2
... [FROM idx1] [TO idx2]
Effect
These additions have the effect that only table rows from row number idx1
,
or up to row number idx2
, are taken into account in the table index used.
If only FROM
is specified, all rows of the table from row number idx1
up to and including the last row are taken into account. If only TO
is specified,
all rows in the table from the first row up to row number idx2
are taken into account.
If the addition USING KEY
is not used, or the
primary table key
is specified in keyname
, the additions FROM
and TO
can only be used for
index tables. In this case, they refer to the row numbers of the
primary table index.
If a sorted
secondary key is
specified in keyname
after USING KEY
, the additions
FROM
and TO
can be used for all table types and refer to the row numbers of the
secondary table index.
idx1
and idx2
are
numerical expression positions of operand type i
. The following limitations apply:
-
If the value of
idx1
is less than 0, it is set to 1 in theLOOP
statement and causes a runtime error in every other statement. If the value ofidx1
is greater than the total number of table rows, no processing takes place. -
If the value of
idx2
is less than 0, theLOOP
statement is not carried out and in every other statement it leads to a runtime error. If the value ofidx2
is greater than the number of table rows, it is set to the number of table rows. -
If the value of
idx2
is less than the value ofidx1
, no processing takes place.
The value of idx1
is evaluated once when the loop is entered. Any changes
to idx1
during loop processing are not taken into account. In contrast, the
value of idx2
is evaluated in each loop pass and any changes made to idx2
during loop processing are taken into account.
Note
To determine when loop processing is exited and whether the value specified in idx2
has been reached, the current line number is evaluated. Note that this number can be changed if lines
are inserted or deleted during a loop pass as described in
LOOP. As a result, there may be more loop passes (if lines are inserted) or fewer loop passes
(if lines are deleted) than is specified by the difference between idx2
and idx1
.
Addition 3
... WHERE log_exp
Effect
Static WHERE
condition. All rows are processed for which the condition after WHERE
is met. You can specify WHERE
for all table categories.
You can specify a logical
expression log_exp
after WHERE
in which the first operand of each operation is a
component of the internal table. You cannot specify a
predicate function.
The components of the internal table must be specified as individual operands and not as part of an
expression. You cannot use parenthesized character-like data objects to specify a component dynamically here. The remaining operands of a comparison are
general expression positions at which any
suitable inidividual operands or
expressions can be specified,
but no components of the internal table. All logical expressions are possible except
IS ASSIGNED
, IS
SUPPLIED, and the obsolete IS REQUESTED
. The specified components can have any data type. The relevant
comparison rules apply to the evaluation.
- When standard tables
are accessed without a secondary key being specified, the access is not optimized. This means that all
rows of the internal table are tested for the logical expression of the
WHERE
addition.
- When using a sorted key or a
hash key (that is, when accessing a
sorted table, a
hashed table, or a
secondary table key),
an attempt is made to optimize the access as described under
Optimization of the
WHERE
Condition. If the corresponding prerequisites are not met:
- the logical expression cannot be transformed to a key access
- the logical expression has the same result as the resulting key access
WHERE
condition cannot contain any duplicate or overlapping key specifications.
Notes
-
When using a
WHERE
condition, note that the comparison rules for incompatible data types apply when comparing incompatible data objects. Here, the data types involved determine which operand is converted. If the additionsWITH TABLE KEY
andWITH KEY
of the statementREAD
are used, however, the content of the specified data objects is always converted to the data type of the columns before the comparison. This can produce varying results. -
If possible, all operands of the logical expression should be in
compatible pairs, so enabling
the
WHERE
condition to be optimized. -
If a selection table
is specified after
IN
as a logical expression, note that the expression at the initial table is always true and then all rows are edited. -
The logical expression declared after
WHERE
is evaluated once when the loop is entered. Any changes to the second operand during loop processing are ignored.
Example
The following example demonstrates the differences in behavior of a WHERE
condition and a key access with WITH TABLE KEY
. With LOOP AT itab WHERE
, the rule for the
comparison of character-like data types applies.
The short column content "AA" is first filled with spaces to change the length to 4. It is then compared
to "AAXX". No matching row is found. With READ TABLE itab WITH TABLE KEY
, the content of text_long
is
converted to the value "AA" before the comparison, by truncating two characters, and then compared to the column content. The result is output without errors.
DATA text_short TYPE c LENGTH 2.
DATA text_long TYPE c LENGTH 4.
DATA itab LIKE TABLE OF text_short WITH NON-UNIQUE KEY table_line.
text_short = 'AA'.
text_long = 'AAXX'.
APPEND text_short TO itab.
LOOP AT itab INTO text_short WHERE table_line = text_long.
ENDLOOP.
WRITE: / 'LOOP:', sy-subrc.
READ TABLE itab INTO text_short WITH TABLE KEY table_line = text_long.
WRITE: / 'READ:', sy-subrc.
Addition 4
... WHERE (cond_syntax)
Effect
Dynamic WHERE
condition. You can declare a character-type data object or
standard table with
character-type data type for cond_syntax
. This data object or standard table
is initial or contains the syntax of a logical expression (in accordance with the rules of the static WHERE
condition) when the statement is executed.
As in ABAP Editor, the syntax in cond_syntax
is not case-sensitive. When
you declare an internal table, you can distribute the syntax across multiple rows. If cond_syntax
is initial when the statement is executed, the logical expression is true. An invalid logical expression raises an exception of the class CX_SY_ITAB_DYN_LOOP.
The obsolete logical operators
(><
, =>
, and =<
) are not supported by cond_syntax
.
Notes
- Of the calculation expressions, only arithmetic expressions are currently supported. The string expressions and bit expressions are not supported. You cannot specify string functions or bit functions currently either.
-
For optimization reasons, the dynamic
WHERE
condition is not evaluated for empty tables. This means that an exception is not raised for empty internal tables if the logical expression contains an error.
Example
Reading lines with particular line numbers in the primary table index that meet a condition. The static and dynamic declaration of a WHERE
condition are shown.
DATA: BEGIN OF line,
col1 TYPE i,
col2 TYPE i,
END OF line.
DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY table_line.
DATA num TYPE i VALUE 400.
DATA dref TYPE REF TO i.
DATA cond TYPE string.
DO 30 TIMES.
line-col1 = sy-index.
line-col2 = sy-index ** 2.
APPEND line TO itab.
ENDDO.
GET REFERENCE OF num INTO dref.
LOOP AT itab INTO line FROM 10 TO 25 WHERE col2 > dref->*.
WRITE / line-col2.
ENDLOOP.
SKIP.
cond = 'col2 > dref->*'.
LOOP AT itab INTO line FROM 10 TO 25 WHERE (cond).
WRITE / line-col2.
ENDLOOP.