Internal Table Basics

Internal table is a data object in ABAP that exists only at run time of a program. It means when the program execution is complete then the internal table will be lost. We use internal table to store database table data after fetching it by a select query. The ABAP run-time system dynamically manages the internal table’s memory. It means we developer do not need to work on memory management of internal table.

Internal table has three parts – rows, columns & work area.
1.       Rows are the line type of internal table. It is a structure which contains several fields. Those fields are of data elements. We need to declare the structure locally or globally to declare the internal table.
2.       Columns are the fields of internal table. Those fields are of different data elements declared by locally or globally.
3.       The most important part of an internal table is its work area. Work area is basically the line type of an internal table. It means it has the same structure of the rows of internal table. Work area contains the same fields of same type of the rows. It is of two types – implicit & explicit work area.
A.      When we declare an internal table with header line then a work area is automatically created with the same name of the table. This work area is called implicit work area which is actually the header line. There is no need to declare work area separately. This work area / header line contains the same table as of the internal table.

Example –
TYPESBEGIN OF ty_mat,
         matnr TYPE mara-matnr,
         werks TYPE marc-werks,
         lgort TYPE mard-lgort,
       END OF ty_mat.

DATA: it_mat TYPE STANDARD TABLE OF ty_mat WITH 
NON-UNIQUE KEY
      matnr WITH HEADER LINE.

Here we have declared a local structure ty_mat. This is the line type / row of the internal table. It means the table will contain rows which has three fields (matnr, werks & lgort). We declare the internal table it_mat with this local structure. We also can declare with global structure.

DATA: it_qinfo TYPE TABLE OF slis_qinfo_alv WITH HEADER LINE
      WITH NON-UNIQUE KEY type.

Here slis_qinfo_alv is a structure which has been declared globally in data dictionary. We can declare the internal table directly with the table type also.

DATA: it_qinfo TYPE slis_t_add_fieldcat WITH HEADER LINE.

Here slis_t_add_fieldcat is a table type declared in data dictionary.

Header line concept:

MATNR                                                WERKS                                              LGORT



 The name of this work area / header line is IT_MAT.

When we create the internal table then it is like following:

MATNR                                                WERKS                                              LGORT















It also contains the same name IT_MAT but it is mentioned IT_MAT[ ] in the program.

B.      If we declare an internal table without header line then we need to declare its work area seperately. Since we are declaring the work area explicitly it is called explicit work area. This work area contains the different name from the internal table.

Example –
TYPESBEGIN OF ty_mat,
         matnr TYPE mara-matnr,
         werks TYPE marc-werks,
         lgort TYPE mard-lgort,
       END OF ty_mat.

DATA: it_mat TYPE STANDARD TABLE OF ty_mat,
      wa_mat TYPE ty_mat.

Similarly we can declare internal table with globally declared structure or table type also.
DATA: it_qinfo TYPE TABLE OF slis_qinfo_alv WITH NON-UNIQUE KEY type.
DATA: it_qinfo TYPE slis_t_qinfo_alv.

Work area concept:

MATNR                                                WERKS                                              LGORT



 The name of this work area is WA_MAT.

When we create the internal table then it is like following:

MATNR                                                WERKS                                              LGORT















The table contains the name IT_MAT.

In today’s programming header line is not used in internal table. It is now obsolete. There are two main reasons for that.
1.       The automatically generated header line / implicit work area has the same name as of internal table. That’s why it loses the readability of program.
2.       When we use nested data objects (internal table has components of structure which is another internal table) then header line is not allowed. In object oriented programming header line is not allowed.

To declare an internal table there is three basic specifications. They are 1. Row type, 2. Key & 3. Types of internal table. Internal table is of three types – standard table, sorted table & hashed table. Standard & sorted tables are called index table because we can access its records by its index. Index is nothing but a row number of the internal table.

1.       Standard table is an index table which has non-unique key. It can be accessed by index or key also. If we want to access by key then the key must be defined otherwise default key would be considered. The declaration is as follows:

DATA: it_mat TYPE STANDARD TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.
OR
DATA: it_mat TYPE TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.

If we don’t mention “Standard table of” clause then by default the system takes it as a standard internal table. We can enter data into a standard internal table by using the APPEND statement. Append always enters data at the last row of the table.

APPEND wa_mat TO it_mat.

2.       Sorted table is another kind of index table which has unique / non unique key. It also can be accessed via index or key. For sorted table the key must be specified. The declaration is as follows:

DATA: it_mat TYPE SORTED TABLE OF ty_mat WITH UNIQUE KEY matnr,
      it_mat TYPE SORTED TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.

Unique key means the MATNR (material no) will must be unique. If same material number is inserted then a run time error will happen. However we can declare the sorted table with non unique key also. In this case same material number can be entered but it will be sorted after entering the number. Here the sorted table behaves similar to sorted standard table. We use INSERT statement to enter any records to the sorted table.

INSERT wa_mat INTO it_mat.

3.       Hashed table is not an index table. It follows the hash algorithm. Here the declaration of key is must and also the key must be unique. Hence no duplicate entry will be in the hashed table. We can access records only by the key.

DATA: it_mat TYPE HASHED TABLE OF ty_mat WITH UNIQUE KEY matnr.

Similar to sorted tables data can be inserted here by INSERT statement. Hashed tables are used when the internal table contains huge volume of data.

INSERT wa_mat INTO TABLE it_mat.

Table kind
Index Tables
Hashed Tables
Standard Table
Sorted Table
Index Access
Yes
Yes
No
Key Access
Yes
Yes
Yes
Key Uniqueness
Non unique
Unique/Non unique
Unique
Usage
Index access
Key access
Only key access

Share this

Related Posts