SAP INSERT ITAB ABAP Statements



Get Example source ABAP code based on a different SAP table
  


ID INSERT-ITAB
• INSERT itab ABAP Statement
• INTO INSERT itab

INSERT itab
Short Reference

ABAP_SYNTAX
INSERT line_spec INTO itab_position $[ result$].

What does it do?
This statement adds one or more lines line_spec at a position itab_position in an internal table. The position can be specified using the primary table key or a table index. result can be used when appending a single line to set a reference to the appended line in the form of a field symbol or a data reference.
When the line is inserted, all existing unique table keys are checked. These can be a unique primary table key and multiple unique secondary table keys. The system handles any duplicates of the different keys according to the following hierarchy: If attempting to insert a single line using a primary key would produce duplicates with respect to the unique primary key, no line is inserted and sy-subrc is set to 4. If attempting to insert a single line using the key or the index would result in duplicates with respect to a unique secondary key, a catchable exception of the class CX_SY_ITAB_DUPLICATE_KEY is raised. If attempting to insert a single line using an index or multiple lines as a block would result in duplicates with respect to a unique primary or secondary key, a runtime error occurs.
System Fields sy-subrcMeaning 0One or more lines were inserted. 4No line was inserted because either a line with the same unique key already existed when inserting single lines using the primary key or the specified index was greater than the current number of lines plus 1 when inserting the lines using the table index.
The system field sy-tabix is not set.



Latest notes:

The value operator VALUE can also be used to construct the content of internal tables.
A special variant INSERT mesh_path can be used to insert lines into the last node of a mesh path.
The administration of an unique secondary table key is updated immediately (direct update) and the administration of a non-unique secondary table key is updated at the next explicit use of the secondary table key ( lazy update). Runtime costs for creating or updating a non-unique secondary table key are not incurred therefore until it is used for the first time.
NON_V5_HINTS
For the latter see the executable example.
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Insertion of single lines into a standard table int_tab using the table index and insertion of references to these lines in a hashed table ref_tab using the table key. The output in the LOOP loops produces the numbers 10 to 1 for int_tab and the numbers 1 to 10 for ref_tab.
ABEXA 00338
ABAP_EXAMPLE_END



Runtime Exceptions



Catchable Exceptions
CX_SY_ITAB_DUPLICATE_KEY
Reason for error:
Duplicate key values in unique secondary key
Runtime error:
ITAB_DUPLICATE_KEY

Non-catchable Exceptions
Reason for error:
When inserting a set of lines, entries with an identical key were produced (the target table is defined by UNIQUE).
Runtime error:
ITAB_DUPLICATE_KEY
Reason for error:
Sort order violated when using an INSERT with index in a sorted table.
Runtime error:
ITAB_ILLEGAL_SORT_ORDER
Reason for error:
Invalid index value ( < = 0) when FROM, TO, or • specified
Runtime error:
TABLE_INVALID_ •
ABAP_NONCAT_END

Return to menu