ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → INSERT itab
Internal Tables - Inserting Rows
This example demonstrates how rows are inserted into internal tables.
Other versions: 7.31 | 7.40 | 7.54
Source Code
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.
<span class="blue">* Fill table</span>
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.
<span class="blue">* Insert a single line into an index table</span>
MOVE itab TO itab1.
line-col1 = 11. line-col2 = 22.
INSERT line INTO itab1 INDEX 2.
INSERT INITIAL LINE INTO itab1 INDEX 1.
cl_abap_demo_services=>list_table( itab1 ).
<span class="blue">* Insert lines into an index table with LOOP</span>
MOVE itab TO itab1.
LOOP AT itab1 INTO line.
line-col1 = 3 * sy-tabix. line-col2 = 5 * sy-tabix.
INSERT line INTO itab1.
ENDLOOP.
cl_abap_demo_services=>list_table( itab1 ).
<span class="blue">* Insert lines into an index table</span>
MOVE itab TO itab1.
MOVE jtab TO jtab1.
INSERT LINES OF itab1 INTO jtab1 INDEX 1.
cl_abap_demo_services=>list_table( jtab1 ).
<span class="blue">* Insert lines into a sorted table</span>
MOVE itab TO itab2.
MOVE jtab TO jtab2.
INSERT LINES OF itab2 INTO TABLE jtab2.
cl_abap_demo_services=>list_table( jtab2 ).
Description
This example is made up of four parts, in which lines 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 above tables to their initial values, using the MOVE
statement, 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
.