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 statement LOOP AT itab with various table keys.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    TYPES:
      BEGIN OF spfli_line,
        carrid   TYPE spfli-carrid,
        connid   TYPE spfli-connid,
        cityfrom TYPE spfli-cityfrom,
        cityto   TYPE spfli-cityto,
      END OF spfli_line.

    DATA(out) = cl_demo_output=>new( ).
    DATA output TYPE TABLE OF spfli_line WITH EMPTY KEY.

    DATA spfli_tab TYPE HASHED TABLE
                   OF spfli_line
                   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 carrid, connid, cityfrom, cityto
           FROM spfli
           ORDER BY carrid, connid
           INTO TABLE @spfli_tab.

    CLEAR output.
    out->next_section( 'LOOP without USING KEY' ).
    LOOP AT spfli_tab ASSIGNING <spfli>.
      output = VALUE #( BASE output ( <spfli> ) ).
    ENDLOOP.
    out->write( output ).

    CLEAR output.
    out->next_section( 'LOOP with USING KEY cityfrom cityto' ).
    LOOP AT spfli_tab ASSIGNING <spfli> USING KEY city_from_to.
      output = VALUE #( BASE output ( <spfli> ) ).
    ENDLOOP.
    out->write( output ).

    CLEAR output.
    out->next_section( 'LOOP with USING KEY cityto cityfrom' ).
    LOOP AT spfli_tab ASSIGNING <spfli> USING KEY city_to_from.
      output = VALUE #( BASE output ( <spfli> ) ).
    ENDLOOP.
    out->write( output ).

    out->display( ).

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 ORDER BY in the SELECT statement.
  • The other two LOOP loops are performed when one of the secondary table keys city_from_to or city_to_from is specified.