sapdev logo background
sapdev logo sapdev logo
Comments

ABAP LOOP AT ITAB COND Statement syntax, information and example SAP source code



Return to Statement index



LOOP AT itab - cond

Short Reference

ABAP Syntax ... [USING KEY keyname ] [FROM idx1] [TO idx2] [WHERE log_exp |(cond_syntax)] ... .

ABAP_ADDITIONS:
1 ... USING KEY keyname
2 ... [FROM idx1] [TO idx2]
3 ... WHERE log_exp
4 ... WHERE (cond_syntax)

What does it do? 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.

ABAP_ADDITION_1 ... USING KEY keyname

What does it do? 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 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:
Specification of a sorted key
The rows are processed by ascending row number in the secondary table index In each loop pass, the system field sy-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 order in which they were inserted into the table. In each loop pass, the system field sy-tabix contains the value 0.
Latest 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 order 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.

Example ABAP Coding See Loop though internal table with key specification .

ABAP_ADDITION_2 ... [FROM idx1] [TO idx2]

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.
Latest notes: 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 .

ABAP_ADDITION_3 ... WHERE log_exp


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 ABAP Coding 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 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 }| ).

ABAP_ADDITION_4 ... WHERE (cond_syntax)


Example ABAP Coding 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.

DO 30 TIMES.
line-col1 = sy-index.
line-col2 = sy-index ** 2.
APPEND line TO itab.
ENDDO.

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 ).
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved




LOOP_AT_ITAB
LOOP_AT_ITAB_RESULT




comments powered by Disqus