Friday 24 November 2017

ABAP – Internal Tables


Internal table is actually a temporary table, which contains the records of an ABAP program that it is being executed. An internal table exists only during the run-time of a SAP program. They are used to process large volumes of data by using ABAP language. We need to declare an internal table in an ABAP program when you need to retrieve data from database tables.

Data in an internal table is stored in rows and columns. Each row is called a line and each column is called a field. In an internal table, all the records have the same structure and key. The individual records of an internal table are accessed with an index or a key. As internal table exists till the associated program is being executed, the records of the internal table are discarded when the execution of the program is terminated. So internal tables can be used as temporary storage areas or temporary buffers where data can be modified as required. These tables occupy memory only at run-time and not at the time of their declaration.

Internal tables only exist when a program is running, so when the code is written, the internal table must be structured in such a way that the program can make use of it. You will find that internal tables operate in the same way as structures. The main difference being that structures only have one line, while an internal table can have as many lines as required.

An internal table can be made up of a number of fields, corresponding to the columns of a table, just as in the ABAP dictionary a table was created using a number of fields. Key fields can also be used with internal tables, and while creating these internal tables they offer slightly more flexibility. With internal tables, one can specify a non-unique key, allowing any number of non-unique records to be stored, and allowing duplicate records to be stored if required.

The size of an internal table or the number of lines it contains is not fixed. The size of an internal table changes according to the requirement of the program associated with the internal table. But it is recommended to keep internal tables as small as possible. This is to avoid the system running slowly as it struggles to process enormous amounts of data.

Internal tables are used for many purposes:

 They can be used to hold results of calculations that could be used later in the program.

 An internal table can also hold records and data so that this can be accessed quickly rather than having to access this data from database tables.

 They are hugely versatile. They can be defined using any number of other defined structures.

Example


Assume that a user wants to create a list of contact numbers of various customers from one or several large tables. The user first creates an internal table, selects the relevant data from customer tables and then places the data in the internal table. Other users can access and use this internal table directly to retrieve the desired information, instead of writing database queries to perform each operation during the run-time of the program.


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 automaticallycreated 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

No comments:

Post a Comment