Entering content frame

Inserting Lines into Tables Locate the document in its SAP Library structure

You can insert one or several lines into internal tables using the INSERT statement.

Inserting a Single Line

To add a line to an internal table, use the statement:

INSERT line INTO TABLE itab.

line is either a work area that is compatible with the line type, or the expression INITIAL LINE. The work area must be compatible because the fields in the table key must be filled from fields of the correct type. INITIAL LINE inserts an initialized blank line suitable for the type.

If the internal table has a unique key, lines whose key already exists in the table will not 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.

Lines are added to internal tables as follows:

        Standard tables

The line is appended to the end of the internal table. The statement works in the same way as the specific statement APPEND for appending lines.

        Sorted tables

The line is inserted into the table according to the table key. If the key is non-unique, duplicates are inserted above the existing entry with the same key. The runtime for the operation increases logarithmically with the number of existing table entries.

        Hashed tables

The table is inserted into the internal hash administration according to the table key.

Inserting Several Lines

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

INSERT LINES OF itab1 [FROM n1] [TO n2] INTO TABLE itab2.

itab1 and itab2 are tables with a compatible line type. The system inserts the lines of table itab1 one by one into table itab2 using the same rules as for single lines.

If itab1 is an index table, you can specify the first and last lines of itab1 that you want to append in n1 and n2.

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

Example

Inserting a Single Line

REPORT demo_int_tables_insert.

DATA: BEGIN OF line,
        land(3)  TYPE c,
        name(10) TYPE c,
        age      TYPE i,
        weight   TYPE p DECIMALS 2,
      END OF line.

DATA itab LIKE SORTED TABLE OF line
          WITH NON-UNIQUE KEY land name age weight.

line-land = 'G'.   line-name   = 'Hans'.
line-age  = 20.    line-weight = '80.00'.

INSERT line INTO TABLE itab.

line-land = 'USA'. line-name   = 'Nancy'.
line-age  = 35.    line-weight = '45.00'.

INSERT line INTO TABLE itab.

line-land = 'USA'. line-name   = 'Howard'.
line-age  = 40.    line-weight = '95.00'.

INSERT line INTO TABLE itab.

line-land = 'GB'.  line-name   = 'Jenny'.
line-age  = 18.    line-weight = '50.00'.

INSERT line INTO TABLE itab.

line-land = 'F'.   line-name   = 'Michele'.
line-age  = 30.    line-weight = '60.00'.

INSERT line INTO TABLE itab.

line-land = 'G'.   line-name   = 'Karl'.
line-age  = 60.    line-weight = '75.00'.

INSERT line INTO TABLE itab.

LOOP AT itab INTO line.
  WRITE: / line-land, line-name, line-age, line-weight.
ENDLOOP.

The list output is:

F   Michele            30             60,00
G   Hans               20             80,00
G   Karl               60             75,00
GB  Jenny              18             50,00
USA Howard             40             95,00
USA Nancy              35             45,00

Single lines are inserted into a sorted internal table.

Example

Inserting Several Lines

REPORT demo_int_tables_insert_lines.

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

DATA: itab LIKE STANDARD TABLE OF line,
      jtab LIKE SORTED TABLE OF line
           WITH NON-UNIQUE KEY col1 col2.

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 TABLE jtab.

LOOP AT jtab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.

The list output is:

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

The example creates two internal tables with the same line type but different table types. Each is filled with three lines. Then, the entire table itab is sorted into the sorted table jtab.

Inserting Lines Using the Index

The INSERT statement allows you not only to insert lines in any type of internal table, but also 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 wa 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 an initialized blank line suitable for the type.

If you use the INDEX option, the new line is inserted before the line that 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 after the last line. 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 the entire 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 n2] INTO itab2
                      [INDEX idx].

n1 and n2 specify the indexes of the first and last lines of itab1.

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.

Example

REPORT demo_int_tables_insert_ind_1.

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 list 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.

ExampleInserting Several Lines Through Index

REPORT demo_int_tables_insert_ind_2.

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 list 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.

Example

REPORT demo_int_tables_insert_ind_3.

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 list 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.

 

Leaving content frame