Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  LOOP AT itab →  LOOP AT itab - Basic Form 

Internal Tables, Loop with Key Specified

The example demonstrates the execution of the LOOP AT itab statement with various table keys.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    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
           ORDER BY carrid, connid
           INTO TABLE @spfli_tab.

    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.

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.