ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  INSERT itab → 

Internal Tables, Insert Rows

This example demonstrates how rows are inserted into internal tables.

Source Code

REPORT demo_int_tables_insert.

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

CLASS demo IMPLEMENTATION.
  METHOD main.

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

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

          itab1 LIKE TABLE OF line,
          jtab1 LIKE itab,
          itab2 LIKE STANDARD TABLE OF line,
          jtab2 LIKE SORTED TABLE OF line
                WITH NON-UNIQUE KEY col1 col2.

* Fill table

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

* Insert a single line into an index table

    itab1 = itab.

    line-col1 = 11. line-col2 = 22.
    INSERT line INTO itab1 INDEX 2.

    INSERT INITIAL LINE INTO itab1 INDEX 1.

    DATA(out) = cl_demo_output=>new(
      )->write_data( itab1 ).

* Insert lines into an index table with LOOP

    itab1 = itab.

    LOOP AT itab1 INTO line.
      line-col1 = 3 * sy-tabix. line-col2 = 5 * sy-tabix.
      INSERT line INTO itab1.
    ENDLOOP.

    out->write_data( itab1 ).

* Insert lines into an index table

    itab1 = itab.
    jtab1 = jtab.

    INSERT LINES OF itab1 INTO jtab1 INDEX 1.

    out->write_data( jtab1 ).

* Insert lines into a sorted table

    itab2 = itab.
    jtab2 = jtab.

    INSERT LINES OF itab2 INTO TABLE jtab2.

    out->write_data( jtab2 ).

    out->display( ).

  ENDMETHOD.
ENDCLASS.

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

Description

This example is made up of four parts, in which rows are inserted in different ways. First, two internal tables, itab and jtab, are filled with squared and cubed numbers. These are also used to reset the tables above to their initial values, using an assignment, between the individual parts of the example.

In the first part, a new row is inserted before the second row and a row with initial values is inserted before the first row.

Next, using a LOOP, a new row is inserted before each existing row.

In the third part, the whole of the table itab1 is inserted before the first row of jtab1.

In the final part, the whole of the table itab2 is inserted into the sorted table jtab2.