ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → INSERT itab
INSERT - line_spec
Other versions: 7.31 | 7.40 | 7.54
Syntax
... wa
| {INITIAL LINE}
| {LINES OF jtab [FROM idx1] [TO idx2] [USING KEY
keyname]} ...
Alternatives
1. ... wa
2. ... INITIAL LINE
3. ... LINES OF jtab [FROM idx1] [TO idx2] [USING KEY keyname]
Effect
Either a work area wa
, an initial row INITIAL LINE
, or multiple rows of an internal table jtab
can be appended.
Alternative 1
... wa
Effect
A new row is created to which the content of the work area wa
is assigned. wa
is a
general expression position. The following applies here:
-
To insert using the table key,
wa
must be compatible with the row type of the internal table. Exceptions to this rule are any constructor expressions whose result type can be incompatible with the row type and whose result is converted to the row type (if possible). -
To insert using the table index,
wa
must be incompatible with the row type of the internal table. wa is converted to the relevant row type, in accordance with the conversion rules.
If a conversion error occurs in the conversion, the exception cannot be handled using CX_SY_CONVERSION_ERROR and the associated runtime error occurs instead. If an
arithmetic expression is specified for wa
, the row type of the internal table is respected when determining the
calculation type.
When inserting individual rows into an internal table with non-unique table keys, the order of the duplicate rows in relation to these keys is determined in accordance with the insertion order of the individual rows. In the case of secondary table keys, this order is determined place during the lazy update.
If there is a conflict with the existing unique primary table key, no row is added and if a key access
occurs, sy-subrc
is set to 4. In an index access occurs, a non-handleable
exception is raised. In the case of a conflict with a unique secondary table key, a handleable exception of the class CX_SY_ITAB_DUPLICATE_KEY is raised.
Notes
-
When using the table key to make inserts, incompatible work areas can be converted to the row type using
the conversion operator
CONV
. -
Specifying a calculation expression for
wa
is usually only a good idea for elementary row types. -
Outside of classes, an obsolete short form is possible where
wa INTO
can be omitted if the internal table has a header lineitab
with the same name. The statement then uses the header line as the work area implicitly.
Example
Inserts a structure constructed using the value operator VALUE
into an internal table.
DATA itab TYPE HASHED TABLE OF scarr WITH UNIQUE KEY carrid.
INSERT VALUE #( carrid = 'XXX'
carrname = 'yyyyyyyyy'
currcode = 'ZZ'
url = '...' ) INTO TABLE itab.
Alternative 2
... INITIAL LINE
Effect
A new row is created in which every component contains the type-dependent initial value.
Example
Inserts an initial row that is associated with a field symbol using the addition
ASSIGNING
. This means that initial rows can be processed directly.
DATA itab TYPE TABLE OF spfli.
FIELD-SYMBOLS <line> LIKE LINE OF itab.
INSERT INITIAL LINE INTO itab INDEX 1 ASSIGNING <line>.
<line>-carrid = '...'.
...
Alternative 3
... LINES OF jtab [FROM idx1] [TO idx2] [USING KEY keyname]
Effect
The rows of an internal table jtab
are added as a block. jtab
is a
functional operand
position. The row types of itab
and jtab
must be compatible when inserting using the table key and must be convertible when inserting using the index.
The inserted rows are sequentially taken from the table jtab
. The order in
which the rows are taken is the same as for the statement
LOOP and can also be influenced by specifying a table key
keyname after USING KEY
. The additions FROM
idx1 and TO idx2
have, in relation to jtab
,
the same syntax and effect as for LOOP
.
When inserting rows as a block into an internal table with non-unique primary table keys, the order of the duplicate rows in relation to this primary key is retained. This does not apply for secondary keys.
- If there is already a duplicate entry in a sorted target table, the duplicates of the source block are inserted in their original order in front of the first duplicate in the target table.
-
When insertions are made in standard tables, however,
INSERT
always operates like the statementAPPEND
and the rows are appended in their original order after the last row as in the additionLINES OF
.
If there is a conflict with an existing unique table key, a handleable exception is raised when inserting multiple rows from an internal table. If a conversion error occurs when rows are inserted, the exception cannot be handled using CX_SY_CONVERSION_ERROR and the associated runtime error occurs instead.
Note
When an internal tables is constructed, the constructor operators
NEW
and
VALUE can also insert multiple rows from a table into the target table using LINES OF
.
Example
Inserts the rows of an internal table itab
into the same table. The rows
are inserted in front of the existing first row and in ascending order due to the sorted secondary key skey
being specified.
DATA itab TYPE TABLE OF i
WITH NON-UNIQUE SORTED KEY skey COMPONENTS table_line.
itab = VALUE #( FOR i = 1 UNTIL i > 10 ( 11 - i ) ).
INSERT LINES OF itab USING KEY skey INTO itab INDEX 1.
cl_demo_output=>display( itab ).