Skip to content

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.

Other versions: 7.31 | 7.40 | 7.54

Source Code

                   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.
    LOOP AT spfli_tab ASSIGNING <spfli>.
      WRITE: / <spfli>-carrid COLOR = 3,
               <spfli>-connid COLOR = 3 INTENSIFIED off,

    WRITE / 'LOOP with USING KEY cityfrom cityto' COLOR = 4.
    LOOP AT spfli_tab ASSIGNING <spfli> USING KEY city_from_to.
      WRITE: / <spfli>-carrid,
               <spfli>-cityfrom COLOR = 3,
               <spfli>-cityto   COLOR = 3 INTENSIFIED off.

    WRITE / 'LOOP with USING KEY cityto cityfrom' COLOR = 4.
    LOOP AT spfli_tab ASSIGNING <spfli> USING KEY city_to_from.
      WRITE: / <spfli>-carrid,
               <spfli>-cityfrom COLOR = 3 INTENSIFIED off,
               <spfli>-cityto   COLOR = 3.


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.