SAP NetWeaver AS ABAP Release 751, ©Copyright 2017 SAP AG. All rights reserved.
ABAP - Keyword Documentation → ABAP - Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → LOOP AT itab → LOOP AT itab - Basic Form →
LOOP AT itab - cond
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.
... USING KEY keyname
Effect
The addition USING KEY can be used to specify a table key keyname with which the processing is carried out. The specified table key influences the order 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 order in which the rows are accessed is as follows:
Notes
Example
The example demonstrates the difference between loops across a standard table of random numbers where a sorted secondary table key is specified and those where it is not. The first loop returns the rows in the order they were appended. The second loop returns the rows sorted in ascending order.
DATA(rnd) = cl_abap_random_int=>create( seed = + sy-uzeit
min = 1
max = 100 ).
DATA itab TYPE STANDARD TABLE OF i WITH EMPTY KEY
WITH NON-UNIQUE SORTED KEY key COMPONENTS table_line.
itab = VALUE #( FOR i = 1 UNTIL i > 10 ( rnd->get_next( ) ) ).
DATA output1 TYPE TABLE OF i WITH EMPTY KEY.
LOOP AT itab INTO DATA(number1).
output1 = VALUE #( BASE output1 ( number1 ) ).
ENDLOOP.
cl_demo_output=>write( output1 ).
DATA output2 TYPE TABLE OF i WITH EMPTY KEY.
LOOP AT itab INTO DATA(number2) USING KEY key.
output2 = VALUE #( BASE output2 ( number2 ) ).
ENDLOOP.
cl_demo_output=>display( output2 ).
Executable Example
Loop Across Internal Table with Key Specified
... [FROM idx1] [TO idx2]
Effect
If these additions are used, only the table rows from row number idx1, or up to row number idx2, are respected if 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 respected.
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 categories and refer to the row numbers of the secondary table index.
idx1 and idx2 are numeric expression positions of operand type i. The following restrictions apply:
The value of idx1 is evaluated once when the loop is entered. Any changes to idx1 during loop processing are ignored. In contrast, the value of idx2 is evaluated in each loop pass and any changes made to idx2 during loop processing are respected.
Note
To determine when loop processing is exited and whether the value specified in idx2
has been reached, the current row number is evaluated. Note that this number can be changed if rows
are inserted or deleted during a loop pass as described in
LOOP. As a result, there may be more loop passes (if rows are inserted) or fewer loop passes
(if rows are deleted) than is specified by the difference between idx2 and idx1.
Example
Determine the primary index of a particular table row using the built-in function line_index and a loop across the internal table from this row.
DATA itab TYPE SORTED TABLE OF scarr
WITH UNIQUE KEY carrid.
SELECT *
FROM scarr
INTO TABLE itab.
DATA(idx) = line_index( itab[ carrid = 'LH' ] ).
IF idx = 0.
RETURN.
ENDIF.
LOOP AT itab FROM idx ASSIGNING FIELD-SYMBOL(<fs>).
...
ENDLOOP.
... WHERE log_exp
Effect
Static WHERE condition. All rows are processed for which the condition after WHERE is met. If a static WHERE condition is specified, the row type of the internal table must be statically identifiable. WHERE can be specified for all table categories.
A logical expression log_exp can be specified after WHERE, in which the first operand of each relational expression is a component of the internal table. Any comparison expression and the predicate expression IS INITIAL can be specified as relational expressions. Other predicates cannot be specified. The components of the internal table must be specified as individual operands and not as part of an expression. Parenthesized character-like data objects cannot be used to specify a component dynamically here. The remaining operands of a relational expression are general expression positions at which any suitable individual operands or expressions can be specified, but no components of the internal table. The specified components can have any data type. The relevant comparison rules apply to the evaluation.
Notes
Example
The following example demonstrates the differences in behavior of a WHERE condition and a key access with WITH TABLE KEY. In LOOP AT itab WHERE, the rule for the comparison of character-like data types applies. The short column content "AA" is first padded with blanks 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 cutting off two characters, and then compared to the column content. The result is produced 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.
cl_demo_output=>write( |LOOP: { sy-subrc }| ).
READ TABLE itab INTO text_short WITH TABLE KEY table_line = text_long.
cl_demo_output=>display( |READ: { sy-subrc }| ).
... WHERE (cond_syntax)
Effect
Dynamic WHERE Condition cond_syntax can be specified as a character-like data object or standard table with character-like row type that, when the statement is executed and with the following exceptions, contains the syntax of a logical expression (in accordance with the rules of the static WHERE condition) or is initial. The following are not supported in a dynamic WHERE condition:
The syntax in cond_syntax is, as in the ABAP Editor, not case-sensitive. When an internal table is specified, the syntax can be distributed across multiple rows. If cond_syntax is initial when the statement is executed, the logical expression is true. Invalid logical expressions raises an exception from the class CX_SY_ITAB_DYN_LOOP.
Security Note
If used wrongly, dynamic programming techniques can present a serious security risk. Any dynamic content that is passed to a program from the outside must be checked thoroughly or escaped before being used in dynamic statements. This can be done using the system class CL_ABAP_DYN_PRG or the predefined function escape. See Security Risks of Input from Outside.
Note
The dynamic WHERE conditions is not evaluated for a blank table for optimization
reasons. Therefore, if an internal table is blank, and a logical expression has errors, no exception is raised.
Example
Gets rows with certain row numbers in the primary table index that meet a condition. Demonstrates the static and dynamic declaration of a WHERE condition.
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 output TYPE TABLE OF string WITH EMPTY KEY.
DATA num TYPE i VALUE 400.
DATA dref TYPE REF TO i.
DATA cond TYPE string.
itab = VALUE #( FOR j = 1 UNTIL j > 30
( col1 = j
col2 = j ** 2 ) ).
dref = REF #( num ).
LOOP AT itab INTO line FROM 10 TO 25 WHERE col2 > dref->*.
APPEND CONV string( line-col2 ) TO output.
ENDLOOP.
APPEND INITIAL LINE TO output.
cond = 'col2 > dref->*'.
LOOP AT itab INTO line FROM 10 TO 25 WHERE (cond).
APPEND CONV string( line-col2 ) TO output.
ENDLOOP.
cl_demo_output=>display_data( output ).