Summary -

In this topic, we described about the below sections -

INSERT statement adds a line/row to the internal table. The data requires to be filled in the table work-area and INSERT statement uses the same work area to insert the data into the internal table. INDEX keyword is used to specify the table index position where the new row to be inserted.

If the table has a record already on the specified index, system inserts the current row at the specified position and existing rows from the specified position reinserted again by incrementing their index by +1.

If the table contains index -1 lines, then the line added at the end.

For standard tables, inserting lines with a non-unique key always successful regardless of the duplicates exists in the table or not. i.e., duplicate rows can be inserted.

For sorted tables, inserting lines with a non-unique key always successful regardless of the duplicates exists in the table or not. i.e., duplicate rows can be inserted.

Inserting lines to sorted table with a unique key throws runtime error if the user attempts to append the line with duplicate key. It also throws runtime error if user violates the sort order of sorted table.

Syntax -

INSERT [<work-area>/INITIAL LINE] INTO TABLE <internal-table>.

OR

INSERT [<work-area>/INITIAL LINE] INTO <internal-table> 
		INDEX <index-num>.

First syntax is used to add the line/row at the end of the internal-table from work-area. Second syntax is used to add the line/row at the index index-num from the work-area.

We need to specify the table index position where we want to insert the new line/row as index-num. INITIAL LINE option initializes all the fields according to the type and inserts the row to the internal table.

Inserting multiple lines -

INSERT statement also inserts the group of lines/records to the internal table. The source can be another internal table.

Syntax -

INSERT LINES OF <itable1> [FROM <index1>] [TO <index2>] 
		INTO TABLE <itable2>.

OR

INSERT LINES OF <itable1> [FROM <index1>] [TO <index2>] 
		INTO <itable2> INDEX <index-num>.

  • itable1 - Specifies the source internal table.
  • Index1 - Specifies the source internal table starting line index (starting row from where to copy).
  • Index2 - Specifies the source internal table ending line index (ending row where the copy stops).
  • Itable2 - Specifies the destination table.
  • Index-num - Specifies the destination table starting index (from where the data records appending from).

SY-SUBRC variable is set to 0 when the rows are successfully inserted.


Example -

Simple example to insert the data to the product information internal table with below structure.

Pid -Character (10)
Pname - Character (40)
Pamount - Decimal (10)


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* internal table Structure creation
TYPES: BEGIN OF t_product,
       pid(10)     TYPE C,
       pname(40)   TYPE C,
       pamount(10) TYPE P,
       END OF t_product.

* Data & internal table declaration
DATA: wa TYPE t_product,
      it TYPE TABLE OF t_product,
      it1 TYPE TABLE OF t_product.

* inserting data to the internal table of INDEX 1
wa-pid     = 'IFB1'.
wa-pname   = 'IFB WASHING MACHINE'.
wa-pamount = 31000.
INSERT wa INTO TABLE it.

* inserting data to the internal table of INDEX 2
wa-pid     = 'IFB2'.
wa-pname   = 'IFB SPLIT AC'.
wa-pamount = 38000.
INSERT wa INTO TABLE it.

WRITE 'displaying IT table data...'.
* Reading internal table for all the records
LOOP AT it INTO wa.
  IF sy-subrc = 0.
    WRITE :/ wa-pid, wa-pname, wa-pamount.
  ELSE.
    WRITE 'No Record Found'.
  ENDIF.
ENDLOOP.

ULINE.
* inserting data to the internal table it1 from another 
* internal table it.
INSERT LINES OF it INTO TABLE it1.

WRITE 'displaying IT1 table data...'.
* Reading internal table for all the records
LOOP AT it1 INTO wa.
  IF sy-subrc = 0.
    WRITE :/ wa-pid, wa-pname, wa-pamount.
  ELSE.
    WRITE 'No Record Found'.
  ENDIF.
ENDLOOP.

Output -

Insert Statement Example output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

t_product - Structure declaration, wa - work area of type t_product and it - internal table declaration of type t_product.

INSERT wa INTO TABLE it - Inserting work area(wa) to the table(it), LOOP AT it INTO wa - Loop to read the internal table to work area record by record.

INSERT LINES OF it INTO TABLE it1 – Inserting records to it1 internal table with it internal table data.