Tuesday, 12 December 2017

Types of internal tables in SAP ABAP ?

Internal table is a data object in ABAP that exists only at run time of a program.

Internal table has three parts
1.       Rows are the line type of internal table.
2.       Columns are the fields of internal table.
3.       The most important part of an internal table is work area  . Work area is basically the line type of an internal table.
         it has the same structure of the rows of internal table. Work area contains the same fields of same type of the rows.

There are three types of internal tables in SAP ABAP programming, an ABAPER  must know and understand before using them in ABAP programs.

1. Standard Internal Tables
2. Sorted Internal Tables
3. Hashed Internal Tables


1. Standard Internal Tables-> These are default internal tables. Standard tables have a linear index. You can access them using either the index or the key.
   To read a record we use either key or index operation. If we want to access the internal table by the key then the key must be defined otherwise default key would be considered.
   To search for a record, we can use either linear search or binary search. We can use insert and append to add records.

TYPES : BEGIN OF TY_MARA,
         MATNR  TYPE MATNR,                                          "Material
         MTART  TYPE MTART,                                          "Material Type
        END OF TY_MARA.

Here we have declared a local structure ty_mara. This is the row of the internal table. It means the table will contain rows which has two field (MATNR & MTART).
We declare the internal table it_mara with this structure. We also can declare with standard structure and customized structure .

The declaration of Standard Internal table is defined as follows :

DATA: IT_MARA TYPE STANDARD TABLE OF TY_MARA WITH NON-UNIQUE KEY MATNR.

                               OR

DATA: IT_MARA TYPE TABLE OF TY_MARA WITH NON-UNIQUE KEY MATNR.

If we don’t enter “Standard” 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.
The key of a standard table is always non-unique, and you may not include any specification for the uniqueness in the table definition.
SO, we can Defined Standard internal Table as .

DATA: IT_MARA TYPE STANDARD TABLE OF TY_MARA .
                             
                                      OR

DATA: IT_MARA TYPE TABLE OF TY_MARA.


2. Sorted Internal Tables-> These are a special type of internal tables, where data is already sorted when we insert the record.
   To read a record we use either key or index operation.
   To search for a record, we use binary search as data is already sorted.
   it's another kind of index table which has unique & non unique key. It also can be accessed via index or key.
   We only use insert, not append we fill the table using the (INSERT) statement, according to the sort sequence defined in the table key.
 
TYPES : BEGIN OF TY_KNA1,
         KUNNR  TYPE KUNNR,                                           "Customer Number
         NAME1  TYPE NAME1_GP,                                        "Name
        END OF TY_KNA1.

Here we have declared a local structure ty_kna1. This is the row of the internal table. It means the table will contain rows which has two field (KUNNR & NAME1).

The declaration of Sorted Internal table is defined as follows :

DATA: IT_KNA1 TYPE SORTED TABLE OF TY_KNA1 WITH UNIQUE KEY KUNNR.

DATA: IT_KNA1 TYPE SORTED TABLE OF TY_KNA1 WITH NON-UNIQUE KEY KUNNR.

Unique key means the KUNNR will must be unique. If same Customer 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 Customer number can be entered but it will be sorted after entering the number.
Here the sorted table behaves like a sorted standard table. We use INSERT statement to enter any records to the sorted table.

3. Hashed Internal Tables-> These are used with logical databases  with all fields and all records.
   Here the index operation is not allowed, we only use key operation.
   To search for a record we use hashed algorithm.
   Here the declaration of internal table  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.
   You cannot access hashed tables using the index.
   Similar to sorted tables data can be inserted here by INSERT statement. Hashed tables are used when the internal table contains huge amount of data.

TYPES : BEGIN OF TY_kNA1,
         KUNNR  TYPE KUNNR,                                           "Customer Number
         NAME1  TYPE NAME1_GP,                                        "Name
        END OF TY_KNA1.

The declaration of Hashed Internal table is defined as follows :

DATA: IT_KNA1 TYPE HASHED TABLE OF TY_KNA1 WITH UNIQUE KEY KUNNR.

Internal tables are not DB tables. Standard and Sorted tables in combined are basically called as Index tables and there nothing else.
                                     
                                         ANY TABLE 
                                               | 
                    ------------------------------------ 
                    |                                              | 
             Index Tables                         Hashed Table 
                     |           
    ------------------------------------  
    |                                               | 
Standard Table                      Sorted Table

No comments:

Post a Comment

In this post we use some basics Events  and Control Break Statements for Generating Classical Report Based On single Table. If You want so...