Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  APPEND 

Internal Table, Appending Rows

This example demonstrates how rows are appended to internal tables.

Other versions: 7.31 | 7.40 | 7.54

Source Code

<span class="blue">* append ... to</span>

    DATA: BEGIN OF wa,
            col1(1) TYPE c,
            col2 TYPE i,
          END OF wa.

    DATA itab LIKE TABLE OF wa.

    DO 3 TIMES.
      APPEND INITIAL LINE TO itab.
      wa-col1 = sy-index. wa-col2 = sy-index ** 2.
      APPEND wa TO itab.
    ENDDO.


    DATA(out) = cl_demo_output=>new(
      )->write_data( itab
      )->line( ).

<span class="blue">* append ... to</span>

    DATA: BEGIN OF line1,
            col1(3) TYPE c,
            col2(2) TYPE n,
            col3    TYPE i,
          END OF line1,
          tab1 LIKE TABLE OF line1.

    DATA: BEGIN OF line2,
            field1(1)  TYPE c,
            field2     LIKE tab1,
          END OF line2,
          tab2 LIKE TABLE OF line2.

    line1-col1 = 'abc'. line1-col2 = '12'. line1-col3 = 3.
    APPEND line1 TO tab1.

    line1-col1 = 'def'. line1-col2 = '34'. line1-col3 = 5.
    APPEND line1 TO tab1.

    line2-field1 = 'A'. line2-field2 = tab1.
    APPEND line2 TO tab2.

    CLEAR tab1.

    line1-col1 = 'ghi'. line1-col2 = '56'. line1-col3 = 7.
    APPEND line1 TO tab1.

    line1-col1 = 'jkl'. line1-col2 = '78'. line1-col3 = 9.
    APPEND line1 TO tab1.

    line2-field1 = 'B'. line2-field2 = tab1.
    APPEND line2 TO tab2.

    LOOP AT tab2 INTO line2.
      out->write_data( line2-field1 ).
      out->write_data( line2-field2 ).
    ENDLOOP.

    out->line( ).

<span class="blue">* append lines of ... to</span>

    DATA: BEGIN OF line,
            col1(1) TYPE c,
            col2 TYPE i,
          END OF line.

    DATA: itab1 LIKE TABLE OF line,
          jtab LIKE itab.

    DO 3 TIMES.
      line-col1 = sy-index. line-col2 = sy-index ** 2.
      APPEND line TO itab1.
      line-col1 = sy-index. line-col2 = sy-index ** 3.
      APPEND line TO jtab.
    ENDDO.

    APPEND LINES OF jtab FROM 2 TO 3 TO itab1.

    out->write_data( itab1 ).

    out->display( ).

Description

In the first part of this example, the internal table itab is created with two columns and filled in the loop DO. At each pass of the loop, a row with initial values is added, after which the table work area is filled with the loop index and the square of the loop index and then appended.

In the second part of this example two internal tables, tab1 and tab2, are created. tab2 has a deep structure because the second component of line2 has the same data type as tab1. line1 is filled and appended to tab1. line2 is filled next and appended to tab2. After tab1 has been initialized using the REFRESH statement, the same procedure is repeated.

Finally, three rows are added to the internal table itab using APPEND statement and the SORTED BY addition. The row with the smallest value in the field col2 is dropped because the number of rows for this APPEND statement is limited to two using INITIAL SIZE.