SAP INSERT ITAB POSITION ABAP Statements



Get Example source ABAP code based on a different SAP table
  


• TABLE INSERT itab
• • INSERT itab

INSERT, itab_position
Short Reference

ABAP_SYNTAX
... ${TABLE itab$}
$| ${itab • idx$}
$| ${itab$} ...

ABAP_ALTERNATIVES:
1 ... TABLE itab
2 ... itab • idx
3 ... itab

What does it do?
These alternatives specify the position in the internal table itab at which lines are inserted. In the variant with the addition TABLE, the position of the insert is specified using the primary table key. In any of the other variants, a line number from the primary table index is used. The latter is only possible for index tables.



Latest notes:

The insert position is determined exclusively using either the primary table key or the primary table index only. For the secondary table keys in the internal table, the system checks for uniqueness and adds the new table line to the corresponding administration (hash administration, secondary table index). In the case of secondary keys, the administration is updated before the next access; for non-unique secondary keys, it is updated before the secondary key is used again.
NON_V5_HINTS
ABAP_HINT_END

ABAP Alternative 1 ... TABLE itab

What does it do?
The lines to be inserted line_spec must be compatible with the line type of the internal table. Depending on the table category, each line is inserted as follows:
For standard tables, each new line is appended as the last line to the internal table regardless of the primary table key.
For sorted tables, each new line is inserted into the sort order of the internal table in accordance with its key values with respect to the primary key. The line numbers in the primary table index of the following lines are increased by one. If the internal table has a non-unique key, duplicate entries are inserted before the existing line.
In hashed tables, each new line is inserted into the internal table by the hash administration in accordance with its key values with respect to the primary key.
If the internal table has one or more unique table keys, no entries are inserted that would produce duplicate entries in one of the unique table keys. When single lines are inserted, sy-subrc is set to 4 if a duplicate entry with respect to the primary key were to be produced, and a catchable exception of the class CX_SY_ITAB_DUPLICATE_KEY is raised if a duplicate entry with respect to a secondary key were to be produced. If multiple lines are inserted, an uncatchable exception is raised.



Latest notes:

If the primary table key is used, it should be noted that this key can be the standard key that covers all character-like and byte-like components if the line type is structured. An empty standard key is possible only for standard tables. In these tables, INSERT always works like APPEND in this variant.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Filling of an internal table connection_tab with data from 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 primary key, any duplicate entries are discarded. The better performing SELECT statement, in which the internal table is specified directly after INTO TABLE, could raise an exception due to the uniqueness of the primary table key.
ABEXA 00342
ABAP_EXAMPLE_END

ABAP Alternative 2 ... itab • idx

What does it do?
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 the line number idx in the primary table index. The line numbers in the primary table index of the following lines are increased by one. idx is a numeric expression position of operand type i.
If idx contains a value that corresponds to the number of existing table lines plus one, the new line is appended as the last line to the internal table. If idx contains a greater value, no line is inserted and sy-subrc is set to 4.
An uncatchable exception is raised in the following cases:
If idx contains a value less than or equal to 0.
If a single line to be inserted would produce a duplicate entry in a unique primary or secondary table key.
If a block of lines to be inserted would produce a duplicate entry in a unique primary table key.
If a line to be inserted would disrupt the sort order of a sorted table. Modifying the sort order with respect to a sorted secondary key, however, never raises an exception. Instead, the associated secondary index is either updated directly (direct update) or updated after a delay (lazy update).
If a single line to be inserted produces a duplicate entry in a unique secondary table key, a catchable exception of the class CX_SY_ITAB_DUPLICATE_KEY is raised.

ABAP_EXAMPLE_VX5
Insertion of a line after a previously searched for line.
ABEXA 00343
ABAP_EXAMPLE_END

ABAP Alternative 3 ... itab

What does it do?
This variant is only possible within a LOOP across the same internal table and if the addition USING KEY is not specified in the LOOP. Each line to be inserted can be inserted in front of the current line in the LOOP.
If the current line was already deleted in the same loop, however, the behavior is undefined.



Latest notes:

This alternative is not recommended. Instead, the addition • should be used to specify the line number explicitly.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Conditional insertion of a line in front of every line in an internal table.
ABEXA 00344
ABAP_EXAMPLE_END

Return to menu