Summary -

Internal tables used for processing the data dynamically during the program execution. So, populating data to the internal table can be done during the program execution. The populating data can be done in three ways by using the below statements -

  • Insert Statement
  • Append Statement
  • Collect Statement

INSERT Statement -

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.


APPEND statement -

The APPEND statement adds a single line/row to an existing internal table. APPEND statement uses the work area to append the line/row information into the table.

APPEND inserts the data after the last line of the internal table. The work area can be either a header line or any other line with the same structure as an internal table.

Syntax -

APPEND [<work-area>/INITIAL LINE] TO <itable>.

  • <wrok-area> - Specifies the work area.
  • itable - Specifies the internal table name.

If the user uses the work area <work-area>, the SAP system adds a new line to the internal table <itable> with the content of work area <work-area>. If the user uses the INITIAL LINE clause, the blank line appends to the table with the initial values in each field according to the definition.

The SY-TABIX variable contains the index number of the appended line after each APPEND statement. For standard tables, appending 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, appending 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. Appending lines to sorted table with a unique key throws runtime error if the user attempts to append the line with a duplicate key. It also throws runtime error if user violates the sort order of sorted table.


Example -

Simple example to insert the data using APPEND 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.

wa-pid     = 'IFB1'.
wa-pname   = 'IFB WASHING MACHINE'.
wa-pamount = 31000.

* Appending data to the internal table
APPEND wa TO it.

* Reading internal table of index 1
READ TABLE it INTO wa INDEX 1.

* If sy-subrc equal to ZERO, the internal table read is successful.
* Otherwise, 'No Record Found' gets displayed.
IF sy-subrc = 0.
  WRITE: wa-pid, wa-pname, wa-pamount.
ELSE.
  WRITE 'No Record Found'.
ENDIF.

Output -

Append 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.

APPEND wa TO it. - Appending work area(wa) to the table(it) and READ TABLE it INTO wa INDEX 1. - Reading internal table with index 1.


COLLECT statement -

COLLECT statement used for populating the internal tables. COLLECT is used while inserting lines into an internal table with unique standard key.

Syntax -

COLLECT [<work-area> INTO] <itable>.

In standard tables, the row is appended at the end of the table. In sorted tables, new row is inserted in the sort order of the internal table according to the key values and the existing table index of the subsequent rows is increased by 1.

If the table defined with header line, INTO option is ignored. If the line is present in the table with the same key and trying to append the same key then new line is not added to the table and throws runtime error.

The SY-TABIX variable contains the index number of the inserted line after each COLLECT statement.


Example -

Simple example to insert the data using COLLECT 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.

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

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

* 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. 

Output -

Collect 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.

COLLECT wa INTO it. - Inserting work area(wa) to the table(it) and READ TABLE it INTO wa INDEX 1. - Reading internal table with index 1.