ABAP Internal Table:Define、Read、Append、Modify、Delete

  Define Data

It is necessary to understand the internal table in order to make and debug the ABAP program. Here’s what the internal table is all about.

What is an internal table?

Briefly, it is a database table that can be shared by multiple programs, saved and reused later, and an internal table that can be used by itself (while the program is running). There are few things that can be done with the internal table alone, and it will often be processed using the work area.

What is a work area?

The internal table is a definition for storing a plurality of data, and the work area is a definition for storing only one row of data. It is often used to read an internal table, store it in the work area, edit it in the work area, and reflect the result in the internal table.

Definition of internal table/work area

First, let’s introduce how to define internal tables and work areas.
The basic method is to use the TYPES command defined in the program as an internal table or work area, or to refer to a database table or structure.
As options for these definitions, there are various definition methods such as making the internal table a standard table, a sort table, a hash table, an internal table with a header, and defining a work area as a field symbol.

Internal table and work area using TYPES statement

Types :
begin of TY_TABLE,
col_1 type I,
col_2 type I,
col_3 type I,
end of TY_TABLE.
DATA:
IT_TAB TYPE TABLE OF TY_TABLE, "Internal Table
WA_TAB TYPE TY_TABLE. "Work Area

Internal table/work area that refers to the database table

DATA:
IT_TAB TYPE TABLE OF MARA, "Internal Table
WA_TAB TYPE MARA. "Work Area

Standard table, sorted table, hash table

DATA:
IT_TAB_STD1   TYPE TABLE OF MARA, "Standard table(Default)
IT_TAB_STD2   TYPE STANDARD TABLE OF MARA, "Standard table
IT_TAB_SORT   TYPE SORT TABLE OF MARA WITH UNIQUE KEY MATNR, "Sort Table
IT_TAB_BINARY TYPE HASH TABLE OF MARA WITH UNIQUE KEY MATNR, "Hash Table

The usage and differences of these will be discussed separately, and this time I will introduce how to use the internal table.

How to use the internal table

Read(LOOP … ENDLOOP、READ TABLE)

Use “LOOP … END LOOP” or “READ TABLE” depending on whether “read” reads multiple rows from the internal table or only one row.

*Read all of records
LOOP AT IT_TAB INTO WA_TAB.
ENDLOOP.

*Read matched records
LOOP AT IT_TAB INTO WA_TAB WHERE MTART = 'ZXXX'.
ENDLOOP.

*Read the first line
READ TABLE IT_TAB INTO WA_TAB INDEX 1.

*Read matched record
READ TABLE IT_TAB INTO WA_TAB WITH KEY MATNR = 'XXXXXXXXX'.

LOOP … ENDLOOP is often used when you want to read the entire internal table, or when you want to read under conditions that are not the primary key. If a primary key is specified as a condition, the same result as READ TABLE can be obtained, but there is no particular advantage. Personally, when you look at the program later, it is recommended that you use it so that you can instantly determine whether you want to acquire multiple lines or the logic you want to acquire only one line. On the other hand, READ TABLE is a command that can not acquire multiple rows but only one row. It is possible to use an optional TRANSPORT NO FIELD instead of INTO WA (work area) and perform only data check when SY-SUBRC is 0 (data existed) or other than 0 (data does not exist or error). it can.

Update(APPEND、MODIFY)

The “update” instruction differs depending on whether a row is added or the value of an existing row is changed. Use APPEND and INSERT commands to add rows. If you want to use a sorted table for aggregation, use the COLLECT command.

APPEND WA_TAB TO IT_TAB.
INSERT WA_TAB INTO IT_TAB.
COLLECT WA_TAB INTO IT_TAB.

It is also possible to add multiple lines at once using “INSERT LINES OF itab1 [FROM n1] [TO n2] INTO TABLE itab2”.
Use the MODIFY instruction to change the value of a row。

LOOP AT IT_TAB INTO WA_TAB.
  WA_TAB-MATNR = 'XXXXXX'.
  MODIFY IT_TAB FROM WA_TAB.
ENDLOOP.

In the above, I am updating explicitly, but I personally think that it is simpler to use a field symbol if you just want to update.

FIELD-SYMBOLS: <FS_TAB> TYPE MARA.

LOOP AT IT_TAB ASSIGNING <FS_TAB>.
  <FS_TAB>-MATNR = 'XXXXXX'.
ENDLOOP.

DELETE

If you want to delete unnecessary lines, specify a condition and use the DELETE command.

DELETE IT_TAB WHERE MATNR = 'XXXXX'.

If you want to delete duplicate rows, use the DELETE statement with the ADJACENT DUPLICATES option. When deleting, it is judged whether the specified items before and after are the same, so please be sure to sort (SORT) immediately before the command.

SORT IT_TAB BY MATNR.
DELETE ADJACENT DUPLICATES FROM IT_TAB COMPARING MATNR. "Delete if duplicate MATNR