SAP® Documentation

Single view

ABAPINSERT_ITAB - INSERT ITAB

rdisp/max_wprun_time - Maximum work process run time   CL_GUI_FRONTEND_SERVICES - Frontend Services  
This documentation is copyright by SAP AG.
SAP E-Book

INSERT itab

Short Reference



Syntax

INSERT line_spec INTO itab_position $[ result$].

Effect

This statement adds one or more rows line_spec to a position itab_position in an internal table. The position can be specified using the primary table key or a table index. Use result when appending a single row to set a reference to the appended row in the form of a field symbol or a data reference.

When the row is in inserted, all existing unique table keys are checked. These can be a primary table key and multiple unique secondary table keys. The system handles any duplicates of the various key according to the following hierarchy:

  1. If attempting to insert a single row using a primary key would produce duplicates with respect to the unique primary key, no row is inserted and sy-subrc is set to 4.

  2. If attempting to insert a single row using the key or the index would result in duplicates with respect to a unique secondary key, a handleable exception of the class CX_SY_ITAB_DUPLICATE_KEY is raised.

  3. If the attempt to insert a single row (using an index) or multiple rows (as a block) would result in duplicates (in terms of a unique primary or secondary key), a runtime error occurs.


System Fields

sy-subrcMeaning
0 (means:One or more rows werer inserted.
4No row was inserted because either a row of the same unique key already existed when inserting single rows using the primary key or the specified index was greater than the current number of rows plus one when inserting the rows using the table index.

The system field sy-tabix is not set.

Notes

  • 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 (see the executable example).
  • The value operatorVALUE can also be used to construct the content of internal tables.
  • A special variant INSERT mesh_path can be used to insert rows into the last node of a mesh path.


Example

Inserts single rows in a standard tableint_tab using the table index and inserts references to these rows in a hashed tableref_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.

TYPES intref type REF TO i.

DATA: int_tab TYPE STANDARD TABLE OF i,
      ref_tab TYPE HASHED TABLE OF intref
              WITH UNIQUE KEY table_line.

DO 10 TIMES.
  INSERT sy-index
         INTO int_tab INDEX 1
         REFERENCE INTO DATA(dref).
  INSERT dref
         INTO TABLE ref_tab.
ENDDO.

cl_demo_output=>begin_section( `Integer Table` ).
LOOP AT int_tab INTO DATA(int).
  cl_demo_output=>write( |{ int }| ).
ENDLOOP.
cl_demo_output=>next_section( `Reference Table` ).
LOOP AT ref_tab INTO dref.
  cl_demo_output=>write( |{ dref->* }| ).
ENDLOOP.
cl_demo_output=>display( ).

Exceptions

Catchable Exceptions

CX_SY_ITAB_DUPLICATE_KEY

  • Cause: Duplicate key values in unique secondary key
    Runtime Error:ITAB_DUPLICATE_KEY

Non-Catchable Exceptions

  • Cause: When inserting a set of rows, entries with an identical key were produced (the target table is defined by UNIQUE).
    Runtime Error:ITAB_DUPLICATE_KEY
  • Cause: Sort order violated when using an INSERT with index in a sorted table.
    Runtime Error:ITAB_ILLEGAL_SORT_ORDER
  • Cause: Invalid index value (<= 0) when FROM, TO, or INDEX specified
    Runtime Error:TABLE_INVALID_INDEX







Addresses (Business Address Services)   General Data in Customer Master  
This documentation is copyright by SAP AG.

Length: 7849 Date: 20200127 Time: 224300     sap01-206 ( 19 ms )

Our Service

Looking for Support? Questions?

The

Consolut

Callback-Service

Leave us your contact details and we will call you back. Panels marked with * are mandatory.