SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → LOOP AT itab →Internal Tables, Loop with Key Specified
The example demonstrates the execution of the LOOP AT itab statement with various table keys.
Source Code
REPORT demo_loop_at_itab_using_key.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA spfli_tab TYPE HASHED TABLE
OF spfli
WITH UNIQUE KEY primary_key
COMPONENTS carrid connid
WITH NON-UNIQUE SORTED KEY city_from_to
COMPONENTS cityfrom cityto
WITH NON-UNIQUE SORTED KEY city_to_from
COMPONENTS cityto cityfrom.
FIELD-SYMBOLS <spfli> LIKE LINE OF spfli_tab.
SELECT *
FROM spfli
INTO TABLE spfli_tab
ORDER BY carrid connid.
WRITE / 'LOOP without USING KEY' COLOR = 4.
SKIP.
LOOP AT spfli_tab ASSIGNING <spfli>.
WRITE: / <spfli>-carrid COLOR = 3,
<spfli>-connid COLOR = 3 INTENSIFIED off,
<spfli>-cityfrom,
<spfli>-cityto.
ENDLOOP.
SKIP.
WRITE / 'LOOP with USING KEY cityfrom cityto' COLOR = 4.
SKIP.
LOOP AT spfli_tab ASSIGNING <spfli> USING KEY city_from_to.
WRITE: / <spfli>-carrid,
<spfli>-connid,
<spfli>-cityfrom COLOR = 3,
<spfli>-cityto COLOR = 3 INTENSIFIED off.
ENDLOOP.
SKIP.
WRITE / 'LOOP with USING KEY cityto cityfrom' COLOR = 4.
SKIP.
LOOP AT spfli_tab ASSIGNING <spfli> USING KEY city_to_from.
WRITE: / <spfli>-carrid,
<spfli>-connid,
<spfli>-cityfrom COLOR = 3 INTENSIFIED off,
<spfli>-cityto COLOR = 3.
ENDLOOP.
SKIP.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
The table spfli_tab is a hashed table with an unique primary key and two non-unique sorted secondary keys.
The first LOOP takes place without a key being specified. The table output is in the order in which the table was filled. This means it is sorted by the fields entered after the ORDER BY of the SELECT statement.
The other two LOOP loops are carried out under the specification of one of the two secondary table keys city_from_to or city_to_from.
The columns whose sort order were used to output the table are indicated by color.