Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  READ TABLE itab →  READ TABLE - result 

Internal Tables, Output Area

This example demonstrates where the row content of internal tables is read to and how.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: BEGIN OF line,
            col1 TYPE i,
            col2 TYPE i,
          END OF line.

    DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.

    DATA subrc TYPE sy-subrc.
    DATA tabix TYPE sy-tabix.

    FIELD-SYMBOLS <fs> LIKE LINE OF itab.

    DATA(out) = cl_demo_output=>new( ).

    itab = VALUE #( FOR j = 1 UNTIL j > 4
            ( col1 = j col2 = j ** 2 ) ).

    out->write_data( itab )->line( ).

<span class="blue">* INTO line COMPARING</span>

    line-col1 = 2.
    line-col2 = 3.

    READ TABLE itab FROM line INTO line COMPARING col2.
    subrc = sy-subrc.

    out->write( |sy-subrc: { subrc }| ).
    out->write_data( line )->line( ).

<span class="blue">* INTO line TRANSPORTING</span>

    CLEAR line.

    READ TABLE itab WITH TABLE KEY col1 = 3
                    INTO line TRANSPORTING col2.
    subrc = sy-subrc.
    tabix = sy-tabix.

    out->write( |sy-subrc: { subrc }|
      )->write( |sy-tabix: { tabix }|
      )->write_data( line
      )->line( ).

<span class="blue">* TRANSPORTING NO FIELDS</span>

    READ TABLE itab WITH KEY col2 = 16  TRANSPORTING NO FIELDS.
    subrc = sy-subrc.
    tabix = sy-tabix.
    out->write( |sy-subrc: { subrc }|
      )->write( |sy-tabix: { tabix }|
      )->line( ).

<span class="blue">* ASSIGNING</span>

    READ TABLE itab WITH TABLE KEY col1 = 2 ASSIGNING <fs>.

    <fs>-col2 = 100.

    out->write_data( itab ).

    out->display( ).

Description

Four alternatives for output behavior when reading internal tables are shown. First a sorted table is filled with a list of square numbers.

In the first alternative, the work area line that is compatible with the row type is filled with the numbers 2 and 3. In the READ statement, the row of the table is found in which the key field col1 has the same content as the work area and is copied to the work area. sy-subrc is two, since different numbers are found when the field col2 is compared.

In the second alternative, the READ statement reads the row of the table in which the key field col1 has the value 3. Only the content of col2 is copied to the work area line. sy-subrc is zero and sy-tabix is three, since itab is an index table.

In the third alternative, the READ statement is used to find the rows of the table in which col2 is 16. The primary table key is not used. No fields are copied to the work area and no rows are assigned a field symbol. Only system fields are set. sy-subrc is zero, since a row was found and sy-tabix is four.

In the READ statement of the final alternative, the rows of the table are read which the key field col1 has the value 2 and are assigned to the field symbol <fs>. The component col2 of <fs> is assigned the value 100. This also changes the corresponding table field.