SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → INSERT itab →
INSERT - itab_position
Syntax
... {TABLE itab}
| {itab INDEX idx}
| {itab} ... .
Alternatives:
1. ... TABLE itab
2. ... itab INDEX idx
3. ... itab
Effect
These alternatives specify at which position of the internal table itab lines
are to be inserted. If you use the option with the addition TABLE, the position
at which the line is inserted is specified using the primary table key. If you use other options, a line number of the table index is used for the specification. The latter option can only be used for
index tables.
Note
The insert position is defined exclusively using the
primary table key or the
primary table index. For the
secondary table key
in the internal table, the system checks whether it is unique and adds the new table row to the corresponding
administration (hash administration, secondary table index). In the case of secondary keys, administration
is updated before the next access to the table, while with non-unique secondary keys, it is updated before the next usage of the secondary key.
... TABLE itab
Effect
The lines line_spec to be inserted must be compatible with the line type of the internal table. Depending on the table type, each line is inserted as follows:
If the internal table has a unique key, duplicate entries are not inserted. When inserting single lines,
sy-subrc is set to 4 if a duplicate entry in the primary key would occur
and a handled exception of the class CX_SY_ITAB_DUPLICATE_KEY is triggered if a duplicate entry in the secondary key would occur. When inserting multiple lines, an exception that cannot be handled is triggered.
Note
When using the primary table key, it is important to note that this may also be the
standard key that covers
all character-like and byte-like components for structured row types. An empty standard key is possible
only for standard tables for which INSERT already works in the same way as APPEND.
Example
Filling an internal table connection_tab with data of the database table spfli. Single lines are inserted using the primary table key and are filled with the content of the work area connection. Since the internal table has a unique key, duplicate entries are rejected. The better performing SELECT statement for which the internal table is specified directly after INTO TABLE could raise an exception due to the uniqueness of the table key.
DATA: BEGIN OF connection,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
distid TYPE spfli-distid,
distance TYPE spfli-distance,
END OF connection.
DATA connection_tab LIKE SORTED TABLE OF connection
WITH UNIQUE KEY cityfrom cityto
distid distance.
SELECT cityfrom cityto distid distance
FROM spfli
INTO connection.
INSERT connection INTO TABLE connection_tab.
ENDSELECT.
... itab INDEX idx
Effect
This variant can only be used for standard tables and sorted tables. Each of the lines to be inserted line_spec is inserted before the line with line number idx in the primary table index. The line numbers for subsequent lines in the primary table index increase by 1. idx is a numerical print position of operand type i.
If idx contains a value equal to the number of the existing table lines plus one, the new line is appended as the last line in the internal table. If idx contains a greater value, no line is inserted and sy-subrc is set to 4.
An exception that cannot be handled is raised when:
If a single line to be inserted results in a duplicate entry in a unique secondary table key, a treatable exception from class CX_SY_ITAB_DUPLICATE_KEY is raised.
... itab
Effect
This variant is only possible within a LOOP loop using the same table and when the USING KEY addition is not specifed for LOOP. Each line to be inserted can be inserted before the current line in the LOOP.
If the current row has already been deleted in the same loop, however, the response is undefined.
Note
The use of this alternative is not recommended. Instead, the line numbers should be specified explicitly with the INDEX addition.