ABAP - Keyword Documentation →  ABAP - Programming Language →  Processing Internal Data →  Internal Tables (itab) →  itab - Processing Statements →  INSERT itab → 
Mail Feedback

INSERT, line_spec

Short Reference

Syntax

... wa
  | {INITIAL LINE}
  | {LINES OF jtab [FROM idx1] [TO idx2] [STEP n] [USING KEY keyname]} ...

Alternatives:

1. ... wa

2. ... INITIAL LINE

3. ... LINES OF jtab [FROM idx1] [TO idx2] [STEP n] [USING KEY keyname]

Effect

Either a work area wa, an initial line INITIAL LINE, or multiple lines of an internal table jtab can be appended.

Alternative 1  

... wa


Effect

A new line is created to which the content of the work area wa is assigned. wa is a general expression position. The following applies here:

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 line type of the internal table is used to determine calculation type.

When inserting individual lines into an internal table with non-unique table keys, the order of the duplicate lines in relation to these keys is determined by the insertion order of the individual lines. In the case of secondary table keys, this occurs during the lazy update.

If there is a conflict with the existing unique primary table key, no line is inserted. If a key access occurs, sy-subrc is set to 4. In an index access, however, an uncatchable exception is raised. In the case of a conflict with a unique secondary table key, a catchable exception of the class CX_SY_ITAB_DUPLICATE_KEY is raised.

Hints



Example

Insertion of 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 line is created in which every component contains its type-dependent initial value.

Example

Insertion of an initial line that is simultaneously linked to a field symbol using the addition ASSIGNING. This means that the initial line 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] [STEP n] [USING KEY keyname]


Effect

The lines of an internal table jtab are appended as a block. jtab is a functional operand position. The line types of itab and jtab must be compatible when inserting using the table key and convertible when inserting using the index.

The lines to be inserted are taken sequentially from the table jtab. The order in which the lines are taken is the same as for the statement LOOP and can also be affected by specifying a table key keyname after USING KEY. The additions FROM idx1 and TO idx2 have the same syntax and effect as for LOOP with respect to jtab. STEP n has the same syntax and effect as for LOOP with the exception that the value of n must be positive.

When inserting lines as a block into an internal table with non-unique primary table keys, the order of the duplicate lines in relation to this primary key is preserved. This does not apply to secondary keys.

If there is a conflict with an existing unique table key, a catchable exception is raised when inserting multiple lines from an internal table. If a conversion error occurs when lines are inserted, the exception cannot be handled using CX_SY_CONVERSION_ERROR and the associated runtime error occurs instead.

Hint

When an internal table is constructed, the constructor operators NEW and VALUE can also insert multiple lines from a table into the target table using LINES OF.

Example

Insertion of the lines of an internal table itab into the same table. The lines are inserted in front of the existing first line and in ascending order due to the specification of 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 ).

Example

Insertion of the lines of an internal table itab into the same table, defined by FROM, TO, and STEP. The lines are inserted in front of the existing first line and in ascending order due to the specification of 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 > 5 ( 6 - i ) ).

INSERT LINES OF itab FROM 1 TO 5 STEP 2
  USING KEY skey INTO itab INDEX 1.

cl_demo_output=>display( itab ).