The INSERT statement for internal tables inserts one or more rows at a specific position or according to the table key. Unlike APPEND, which always appends at the end, INSERT enables precise control over the insertion position.
Syntax
1. Insert Row by Index
INSERT <row> INTO <internal_table> INDEX <index>.2. Insert Row According to Key
INSERT <row> INTO TABLE <internal_table>.3. Insert Multiple Rows
INSERT LINES OF <source_table> INTO TABLE <target_table>.INSERT LINES OF <source_table> FROM <from> TO <to> INTO <target_table> INDEX <index>.4. Insert Initial Row
INSERT INITIAL LINE INTO <internal_table> INDEX <index>.INSERT INITIAL LINE INTO TABLE <internal_table>.System Fields
After INSERT:
-
sy-subrc:0: Row successfully inserted4: Row could not be inserted (e.g., duplicate with UNIQUE KEY)
-
sy-tabix: Index of the inserted row (with INDEX variant)
Examples
1. Insert Row by Index
DATA: lt_names TYPE TABLE OF string.
lt_names = VALUE #( ( `Anna` ) ( `Clara` ) ( `David` ) ).
" Insert Bernd at position 2INSERT `Bernd` INTO lt_names INDEX 2.
" Result: Anna, Bernd, Clara, DavidLOOP AT lt_names INTO DATA(lv_name). WRITE: / sy-tabix, lv_name.ENDLOOP.2. Insert at Beginning
DATA: lt_numbers TYPE TABLE OF i.
lt_numbers = VALUE #( ( 2 ) ( 3 ) ( 4 ) ).
" Insert 1 at beginningINSERT 1 INTO lt_numbers INDEX 1.
" Result: 1, 2, 3, 43. Insert Structure
TYPES: BEGIN OF ty_customer, id TYPE i, name TYPE string, city TYPE string, END OF ty_customer.
DATA: lt_customers TYPE TABLE OF ty_customer, ls_customer TYPE ty_customer.
lt_customers = VALUE #( ( id = 1 name = 'Miller' city = 'Berlin' ) ( id = 3 name = 'Weber' city = 'Hamburg' )).
" Insert new customer at position 2ls_customer = VALUE #( id = 2 name = 'Schmidt' city = 'Munich' ).INSERT ls_customer INTO lt_customers INDEX 2.
" Result: Miller (1), Schmidt (2), Weber (3)4. INSERT INTO TABLE (by Key)
DATA: lt_sorted TYPE SORTED TABLE OF ty_customer WITH UNIQUE KEY id.
lt_sorted = VALUE #( ( id = 1 name = 'Miller' city = 'Berlin' ) ( id = 3 name = 'Weber' city = 'Hamburg' )).
" Automatically inserted at correct position (sorted by key)ls_customer = VALUE #( id = 2 name = 'Schmidt' city = 'Munich' ).INSERT ls_customer INTO TABLE lt_sorted.
IF sy-subrc = 0. WRITE: / 'Successfully inserted at position:', sy-tabix.ENDIF.
" Result: Automatically sorted between ID 1 and 35. Preventing Duplicates (UNIQUE KEY)
DATA: lt_unique TYPE SORTED TABLE OF ty_customer WITH UNIQUE KEY id.
lt_unique = VALUE #( ( id = 1 name = 'Miller' city = 'Berlin' ) ).
" Attempt to insert duplicatels_customer = VALUE #( id = 1 name = 'New' city = 'Cologne' ).INSERT ls_customer INTO TABLE lt_unique.
IF sy-subrc = 4. WRITE: / 'Duplicate! Row not inserted.'.ENDIF.6. HASHED TABLE
DATA: lt_hashed TYPE HASHED TABLE OF ty_customer WITH UNIQUE KEY id.
" For HASHED TABLE: Always INSERT INTO TABLE (no INDEX!)ls_customer = VALUE #( id = 5 name = 'Bauer' city = 'Dresden' ).INSERT ls_customer INTO TABLE lt_hashed.
IF sy-subrc = 0. WRITE: / 'Inserted into hash table.'.ENDIF.7. Insert Multiple Rows (LINES OF)
DATA: lt_source TYPE TABLE OF string, lt_target TYPE TABLE OF string.
lt_source = VALUE #( ( `New1` ) ( `New2` ) ( `New3` ) ).lt_target = VALUE #( ( `Old1` ) ( `Old2` ) ).
" Insert all rows from source into target (at end)INSERT LINES OF lt_source INTO TABLE lt_target.
" Result: Old1, Old2, New1, New2, New38. LINES OF with INDEX
DATA: lt_source TYPE TABLE OF string, lt_target TYPE TABLE OF string.
lt_source = VALUE #( ( `X` ) ( `Y` ) ( `Z` ) ).lt_target = VALUE #( ( `A` ) ( `B` ) ( `C` ) ).
" Insert rows at position 2INSERT LINES OF lt_source INTO lt_target INDEX 2.
" Result: A, X, Y, Z, B, C9. LINES OF with FROM…TO
DATA: lt_source TYPE TABLE OF i, lt_target TYPE TABLE OF i.
lt_source = VALUE #( ( 10 ) ( 20 ) ( 30 ) ( 40 ) ( 50 ) ).lt_target = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).
" Only insert rows 2-4 from sourceINSERT LINES OF lt_source FROM 2 TO 4 INTO TABLE lt_target.
" Result: 1, 2, 3, 20, 30, 4010. Insert INITIAL LINE
DATA: lt_customers TYPE TABLE OF ty_customer.
lt_customers = VALUE #( ( id = 1 name = 'Miller' city = 'Berlin' )).
" Insert empty row at position 1INSERT INITIAL LINE INTO lt_customers INDEX 1.
" Result: (empty), MillerLOOP AT lt_customers INTO DATA(ls_cust). WRITE: / ls_cust-id, ls_cust-name.ENDLOOP.11. INSERT with ASSIGNING
DATA: lt_customers TYPE TABLE OF ty_customer.
" Insert row and edit directlyINSERT VALUE #( id = 99 ) INTO TABLE lt_customers ASSIGNING FIELD-SYMBOL(<ls_new>).
IF sy-subrc = 0. <ls_new>-name = 'Newly created'. <ls_new>-city = 'Unknown'.ENDIF.12. INSERT with REFERENCE INTO
DATA: lt_data TYPE TABLE OF ty_customer, lr_line TYPE REF TO ty_customer.
INSERT VALUE #( id = 100 name = 'Test' ) INTO TABLE lt_data REFERENCE INTO lr_line.
IF sy-subrc = 0. lr_line->city = 'Reference update'.ENDIF.13. INSERT in LOOP (Caution!)
" WARNING: INSERT in LOOP can cause infinite loops!DATA: lt_numbers TYPE TABLE OF i.
lt_numbers = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).
" WRONG - Can cause infinite loop!" LOOP AT lt_numbers INTO DATA(lv_num)." IF lv_num = 2." INSERT 99 INTO lt_numbers INDEX sy-tabix." ENDIF." ENDLOOP.
" CORRECT: Collect changes and insert afterwardsDATA: lt_to_insert TYPE TABLE OF i.LOOP AT lt_numbers INTO DATA(lv_num). IF lv_num = 2. APPEND 99 TO lt_to_insert. ENDIF.ENDLOOP.INSERT LINES OF lt_to_insert INTO lt_numbers INDEX 2.14. INSERT for Different Table Types
| Table Type | INSERT … INDEX | INSERT INTO TABLE |
|---|---|---|
| STANDARD | Yes | Yes (at end) |
| SORTED | No (syntax error) | Yes (sorts in) |
| HASHED | No (syntax error) | Yes (by hash) |
" STANDARD TABLE: Both variants possibleDATA: lt_standard TYPE STANDARD TABLE OF ty_customer.INSERT ls_customer INTO lt_standard INDEX 1. " OKINSERT ls_customer INTO TABLE lt_standard. " OK (at end)
" SORTED TABLE: Only INTO TABLEDATA: lt_sorted TYPE SORTED TABLE OF ty_customer WITH UNIQUE KEY id." INSERT ls INTO lt_sorted INDEX 1. " SYNTAX ERROR!INSERT ls_customer INTO TABLE lt_sorted. " OK (sorts in)
" HASHED TABLE: Only INTO TABLEDATA: lt_hashed TYPE HASHED TABLE OF ty_customer WITH UNIQUE KEY id." INSERT ls INTO lt_hashed INDEX 1. " SYNTAX ERROR!INSERT ls_customer INTO TABLE lt_hashed. " OKINSERT vs. APPEND
| Aspect | INSERT | APPEND |
|---|---|---|
| Position | Arbitrary (INDEX) or by key | Always at end |
| SORTED TABLE | INTO TABLE (sorts in) | Only if sorting preserved |
| HASHED TABLE | INTO TABLE | Not possible |
| Duplicate check | sy-subrc = 4 for UNIQUE KEY | None (except SORTED) |
| Performance | With INDEX: O(n) | O(1) |
" APPEND: Always at endAPPEND ls_customer TO lt_standard.
" INSERT INDEX: At specific positionINSERT ls_customer INTO lt_standard INDEX 1.
" INSERT INTO TABLE: According to table typeINSERT ls_customer INTO TABLE lt_sorted. " Automatically sorts inINSERT vs. MODIFY
" INSERT: Inserts new row (error on duplicate)INSERT ls_customer INTO TABLE lt_unique.
" MODIFY: Inserts OR updates existingMODIFY TABLE lt_unique FROM ls_customer.Performance Tips
-
Mass insert with LINES OF:
" SLOW: Individual INSERTsLOOP AT lt_source INTO ls_line.INSERT ls_line INTO TABLE lt_target.ENDLOOP." FASTER: LINES OFINSERT LINES OF lt_source INTO TABLE lt_target. -
SORTED vs. STANDARD for frequent inserts:
" For many inserts: STANDARD TABLE collect, then sortDATA: lt_collect TYPE STANDARD TABLE OF ty_data.LOOP AT lt_input INTO ls_input.APPEND ls_input TO lt_collect.ENDLOOP.SORT lt_collect BY key_field.DELETE ADJACENT DUPLICATES FROM lt_collect COMPARING key_field. -
INDEX insert from back:
" For multiple index inserts: From back to front" to minimize shiftsINSERT 'C' INTO lt_names INDEX 3.INSERT 'B' INTO lt_names INDEX 2.INSERT 'A' INTO lt_names INDEX 1.
Important Notes / Best Practice
INSERT ... INDEXonly possible for STANDARD TABLE.INSERT INTO TABLErespects the table key (sorts for SORTED, hashes for HASHED).- For
UNIQUE KEY: Checksy-subrcfor duplicate detection. - Avoid
INSERTinLOOP AT– danger of infinite loops! - Use
LINES OFfor mass inserts (more performant than individual inserts). ASSIGNINGandREFERENCE INTOenable direct access to the inserted row.- For database tables, use
INSERT INTO dbtab. APPENDis faster if position doesn’t matter (at end).MODIFYcombines INSERT and UPDATE in one statement.- For
SORTED TABLE, rows are automatically inserted at the correct position.