ABAP Internal Table

by | Jan 14, 2019 | ABAP Beginner

Home » SAP » ABAP » ABAP Beginner » ABAP Internal Table

Preface – This post is part of the ABAP Beginner series.

Very often, there is a situation in ABAP where we need to store multiple data of a definite structure at runtime and perform some operations upon them. For runtime operations we cannot hit database every time. In that situation, we fetch required data from database table, store it in something called ABAP Internal Table and perform operations upon them.

Definition

An Internal table in ABAP is a runtime copy of a table or structure. It is a reusable object. Each line of an internal table has same structure. In SAP, Internal table is an alternative to Arrays.

ABAP Internal Table

ABAP Internal Table – Illustration Image

Syntax

DATA:  <YOUR_INTERNAL_TABLE_NAME> TYPE TABLE OF <Structure, Table, Data type, Table Type>.

Note: An Internal table can also be defined at runtime. It is explained in one of the example below.

Examples

Case 1:

DATA:  itab_emp TYPE TABLE OF   zemployee. // Declaration of Internal table
SELECT * FROM zemployee INTO TABLE itab_emp. // feeding data in internal table

Case 2:

SELECT * FROM zemployee INTO TABLE @DATA (itab_emp). // Declaration of Internal table at runtime and feeding data there itself

Types of ABAP Internal Tables

According to the frequency of use, internal tables are of following types:

TypeDescriptionExample
Standard tablesA standard Internal table is the one which is accessed by its index. In this table we APPEND data one by one and later access data (Read, Modify and Delete) using INDEX statement.DATA:  itab_emp TYPE TABLE OF   zemployee.
Sorted tablesA sorted internal table is just like a standard internal table except data is inserted not appended. It is sorted as we fill it. We fill sorted table using INSERT statement.DATA:  itab_emp TYPE SORTED TABLE OF zemployee WITH UNIQUE KEY empid.
Hashed tablesA hashed internal table is the one in which data is filled using UNIQUE KEY. To access data we need this key.DATA:  itab_emp TYPE HASHED TABLE OF zemployee

WITH UNIQUE KEY empid empname.

Operations on ABAP Internal Tables

Operations on Entire Internal Tables

Assigning Internal TablesMOVE itab1 TO itab2.

OR

itab2 = itab1.

Initialize Internal TablesCLEAR itab => To clear the header of the table

CLEAR itab [] => To initialize the table but the initial memory remains reserved.

REFRESH itab => Clears data of table but the initial memory remains reserved. It is same as CLEAR itab [].

FREE itab =>It clears data as well as the initial memory allocated to the table.

Compare Internal TablesSuppose we have two Internal tables ITAB1 and ITAB2 with same data type but different number of data, let us say 2 and 3 Lines of data respectively. The statement returns Boolean result.

ITAB1 GT ITAB2 (Greater than) => False

 

ITAB1 EQ ITAB2 (Equal to) => False

 

ITAB1 LE ITAB2 ( Less than or equal to) => True

 

ITAB1 NE ITAB2 (Not Equal to) => True

 

ITAB1 LT ITAB2 (Less than) => True

Sort Internal TablesSORT itab [ASCENDING|DESCENDING] [AS text] [STABLE]. The default is ascending order.
Internal Tables as Interface ParametersWe can use Internal table as a parameter in Forms and Interfaces. In forms Internal Table is written in addition to USING and CHANGING.
Determining the Attributes of Internal TablesDESCRIBE TABLE itab [LINES lv_lin] [OCCURS lv_n] [KIND lv_knd].

If you use LINES, it gives number of rows in Internal table.

If you use OCCURS, it gives initial size of Internal table.

If you use KIND, it gives type of Internal table.

 

 

Operations on Individual Lines

 
Access Methods for Individual Table Entries1. Using Work Area

READ TABLE <itable> [INTO <wa>] INDEX <idx>.

2. Field Symbol

READ TABLE <itable> ASSINGING FIELD SYMBOL(<fs_tab>)

Filling Internal Tables Line by LineAPPEND [<wa> TO / INITIAL LINE TO] <itab>.
Reading Table LinesREAD TABLE itab FROM wa result.

 

or using a key

 

READ TABLE itab WITH TABLE KEY k1 = f1 … kn = fn result.

 

Both the above statements set sy-subrc value to 0 & 4 for success and failure search cases respectively.

Processing Table Lines in LoopsLOOP AT itab result condition.

statement block

ENDLOOP.

The result part can be work area or field symbol.

Changing Table LinesMODIFY TABLE itab FROM wa [TRANSPORTING f1 f2 …] [WHERE condition].

The given statement modifies single record based on condition.

In Transporting we can give specific column/field we want to modify.

Deleting Table LinesDELETE TABLE itab FROM wa.

 

or using a key

 

DELETE TABLE itab WITH TABLE KEY k1 = f1 … kn = fn.

Searching Through Internal Tables Line by Line1.  FIND IN TABLE

FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern IN TABLE itab [IN {BYTE|CHARACTER} MODE]

·         [{FIRST OCCURRENCE}|{ALL OCCURRENCES} tells if search is only for first findings or for all

·         “pattern” is the string being searched for

·         [IN {BYTE|CHARACTER} MODE] tells if the search will be character wise or byte wise

 

2.  REPLACE IN TABLE

REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern   IN TABLE itab … WITH new [IN {BYTE|CHARACTER} MODE]

·         The way FIND works in same way, it also finds and replaces the pattern with new

*** We will discuss all these operations in a separate post for better clarification.

Open SQL in ABAP Internal Table

The way we access and manipulate data in tables of SAP ABAP, in the same way we can use open SQL to manipulate data of internal tables

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author