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 how, and where to, the row contents of internal tables are read.

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 behaviour during the reading of internal tables are shown. First a sorted table is filled with a list of square numbers.

In the first alternative, the work area line, which is compatible with the row type, is filled with the numbers 2 and 3. Using the READ statement the rows of the table are found, in which the keyfield col1 has the same contents as the work area, and these are then copied. sy-subrc is two because different numbers were found during the comparison of field col2.

In the second alternative, the READ statement is used to read those rows in which the keyfield col1 has the value 3. Only the content of col2 is copied to the work area line. sy-subrc has the value zero and sy-tabix is three because 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 because a row has been found and sy-tabix is four.

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