ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  MODIFY itab →  MODIFY itab - itab_line → 

Internal tables, index assess with key specification

The example shows an index access to a hashed table.

Source Code

REPORT demo_modify_table_using_key.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA: sflight_tab TYPE HASHED TABLE
                      OF sflight
                      WITH UNIQUE KEY primary_key
                        COMPONENTS carrid connid fldate
                      WITH NON-UNIQUE SORTED KEY plane_type
                        COMPONENTS planetype,
          sflight_wa  LIKE LINE OF sflight_tab,
          count       TYPE i.

    SELECT *
           FROM sflight
           INTO TABLE sflight_tab
           WHERE carrid = 'LH'.

    LOOP AT sflight_tab INTO sflight_wa USING KEY plane_type
                                        WHERE planetype = 'A310-300'.
      sflight_wa-seatsmax = sflight_wa-seatsmax + 20.
      MODIFY sflight_tab INDEX sy-tabix
                         USING KEY loop_key
                         FROM  sflight_wa
                         TRANSPORTING seatsmax.
      IF sy-subrc = 0.
        count = count + 1.
      ENDIF.
    ENDLOOP.

    cl_demo_output=>display( |{ count } flights modified| ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

The table sflight_tab is a hashed table with a unique primary key and a non-unique secondary sorted key. Since a secondary sorted key is specified in the MODIFY statement after USING KEY, it is possible to access the hashed table via the associated secondary table index.

The example only serves to demonstrate the syntax. Generally, instead of using the MODIFY statement such modifications should be carried out using a field symbol or a data reference:

LOOP AT sflight_tab ASSIGNING <sflight_wa> USING KEY plane_type
                    WHERE planetype = 'A310-300'.
  <sflight_wa>-seatsmax = <sflight_wa>-seatsmax + 20.
ENDLOOP.