Inserting Lines Using the Index 

The INSERT statement allows you not only to insert lines in any type of internal table, but also allows you to change them using a line index. You can insert either a single line or a group of lines into index tables using the index.

Inserting a Single Line

To insert a line into an index table, use the statement:

INSERT <line> INTO <itab> [INDEX <idx>].

<line> is either a work area that is convertible to the line type, or the expression INITIAL LINE. If you use <wa>, the system adds a new line to the internal table <itab> and fills it with the contents of the work area. INITIAL LINE inserts a blank line containing the correct initial value for each field of the structure.

If you use the INDEX option, the new line is inserted before the line which has the index <idx>. After the insertion, the new entry has the index <idx> and the index of the following lines is incremented by 1. If the table contains <idx> -1 lines, the new line is added at the end of the table. If the table has less than <idx> - 1 lines, the new line cannot be inserted, and SY-SUBRC is set to 4. When the system successfully adds a line to the table, SY-SUBRC is set to 0.

Without the INDEX addition, you can only use the above statement within a LOOP. Then, the new line is inserted before the current line (<idx> is implicitly set to SY-TABIX).

Appending lines to standard tables and sorted tables with a non-unique key works regardless of whether lines with the same key already exist in the table. Duplicate entries may occur. A runtime error occurs if you attempt to add a duplicate entry to a sorted table with a unique key. Equally, a runtime error occurs if you violate the sort order of a sorted table by appending to it.

Inserting Several Lines

To add several lines to an internal table, use the statement:

INSERT LINES OF <itab1> INTO <itab2> [INDEX <idx>].

The system inserts the lines of table <itab1> one by one into <itab2> using the same rules as for single lines. ITAB1 can be any type of table. The line type of ITAB1 must be convertible into the line type of ITAB2.

When you append an index table to another index table, you can specify the lines to be appended as follows:

INSERT LINES OF <itab1> [FROM <n1>] [TO <n 2>] INTO <itab2>
                        [INDEX <idx>].

<n 1 > and <n 2 > specify the indexes of the first and last lines of ITAB1 that you want to insert into ITAB2.

Depending on the size of the tables and where they are inserted, this method of inserting lines of one table into another can be up to 20 times faster than inserting them line by line in a loop.

Examples

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE TABLE OF LINE.

DO 2 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
ENDDO.

LINE-COL1 = 11. LINE-COL2 = 22.
INSERT LINE INTO ITAB INDEX 2.

INSERT INITIAL LINE INTO ITAB INDEX 1.

LOOP AT ITAB INTO LINE.
  WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1          0          0
         2          1          1
         3         11         22
         4          2          4

The example creates an internal table ITAB and fills it with two lines. A new line containing values is inserted before the second line. Then, an initialized line is inserted before the first line.

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE TABLE OF LINE.

DO 2 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
ENDDO.

LOOP AT ITAB INTO LINE.
  LINE-COL1 = 3 * SY-TABIX. LINE-COL2 = 5 * SY-TABIX.
  INSERT LINE INTO ITAB.
ENDLOOP.

LOOP AT ITAB INTO LINE.
  WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1         3          5
         2         1          1
         3         9         15
         4         2          4

The example creates an internal table ITAB and fills it with two lines. Using a LOOP construction, the program inserts a new line before each existing line.

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA: ITAB LIKE TABLE OF LINE,
      JTAB LIKE ITAB.

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 LINES OF ITAB INTO JTAB INDEX 1.

LOOP AT JTAB INTO LINE.
  WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.
ENDLOOP.

The output is: :

         1         1          1
         2         2          4
         3         3          9
         4         1          1
         5         2          8
         6         3         27

The example creates two internal tables of the same type. Each is filled with three lines. Then, the entire table ITAB is inserted before the first line of JTAB.