ABAP Addition 1 ... USING KEY keyname> 2 ... $[FROM idx1$] $[TO idx2$]> 3 ... STEP n> 4 ... WHERE log_exp> 5 ... WHERE (cond_syntax)>
What does it do? These optional additions to LOOP AT itab>> specify a subset of lines to be processed and the order in which the loop is executed.
The subset of lines can be defined with:
FROM> and TO> that define an interval of line numbers for index tables
WHERE> that selects lines that fulfill a condition specified in a logical expression
STEP> that allows lines to be skipped by defining a step size greater than 1.
The processing order can be defined with:
USING KEY keyname>> that defines the table key that is used for the loop and thus overrides the standard order determined by the table category.
STEP> that can define a reverse order by specifying a negative step size. If none of the conditions are specified, all table lines are read in the standard order that is defined by the table category as described under LOOP AT itab>>. • USING KEY LOOP AT itab
ABAP Addition
What does it do? The optional addition USING KEY> specifies a table key keyname>> with which the processing is executed. The specified table key affects the order in which the table lines are accessed, and the evaluation of the other conditions. If the primary table key> is specified using its name primary_key>, the processing behaves in the same way as if no key were explicitly specified. If a secondary table key> is specified, the order in which the lines are accessed is as follows. ABAP Addition
Specification of a sorted key>> The lines are processed by ascending line number in the secondary table index> In each loop pass, the system field sy-tabix> contains the line number of the current line in the associated secondary table index.
Specification of a hash key>> The lines 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. A preceding sort using the statement SORT>> does not change this processing order. The order defined by USING KEY> can be reversed by using the addition STEP> with a negative step size. Within the loop, the key used can be addressed using the predefined name loop_key>. This is possible in all statements where the table key keyname>> to be used can be specified explicitly. This type of statement must then be listed in the loop itself. Including the statement in a procedure that is called in the loop is not sufficient.
Latest notes:
The fact that the processing order defined by a secondary hash key is not affected by a preceding sort using the statement SORT> is a different behavior than for the processing order defined by the primary key of a hashed table that is affected by a preceding sort.
If a secondary table key is specified, a simultaneously specified WHERE> condition also must be optimizable>, otherwise a syntax error occurs or an exception is raised. NON_V5_HINTS ABAP_HINT_END
ABAP_EXAMPLE_VX5 The example demonstrates the difference between loops across a standard table of random numbers with and without a sorted secondary table key specification. The first loop returns the lines in the order in which they were appended. The second loop returns the lines sorted in ascending order. ABEXA 00390 ABAP_EXAMPLE_END
ABAP_EXAMPLE_ABEXA Loop Across Internal Table with Key Specified> ABAP_EXAMPLE_END • FROM idx LOOP AT itab • TO idx LOOP AT itab