You can insert lines into internal tables either singly or in groups:
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 are must be
compatible because the fields in the table key must be filled from fields of the correct type. INITIAL LINE inserts a blank line containing the correct initial value for each field of the structure.If the table has a unique key and you attempt to insert lines whose key already exists in the table, the system does not add the line to the table, and sets SY-SUBRC 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:
The line is
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.
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 <n 2>] 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 <itab2> using the same rules as for single lines.
If <itab1> is an index table, you can specify the first and last lines of the table that you want to append in <n
1 > and <n 2 >.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,
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 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
The example fills a sorted internal table with six entries.
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 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, ITAB is sorted into the sorted table JTAB.