Example of Internal Tables

The following program is an example of how to declare structured data objects as internal tables.

PROGRAM SAPMZTST.

TYPES: BEGIN OF MYSTRING,
NUMBER TYPE I,
NAME(10) TYPE C,
END OF MYSTRING.

TYPES MYTAB TYPE MYSTRING OCCURS 5.

DATA STRING TYPE MYSTRING.
DATA ITAB TYPE MYTAB.

STRING-NUMBER = 1. STRING-NAME = 'John'.    
APPEND STRING TO ITAB.
STRING-NUMBER = 2. STRING-NAME = ‘Paul’.    
APPEND STRING TO ITAB.
STRING-NUMBER = 3. STRING-NAME = 'Ringo'.  
APPEND STRING TO ITAB.
STRING-NUMBER = 4. STRING-NAME = 'George'.
APPEND STRING TO ITAB.

LOOP AT ITAB INTO STRING.
   WRITE: / STRING-NUMBER,STRING-NAME.
ENDLOOP.

This program produces the following output on the screen:

         1  John

         2  Paul

         3  Ringo

         4  George

In this example, first a data type MYSTRING is defined as a structure. Then, based on the structure MYSTRING, a data type MYTAB is defined as an internal table with the OCCURS parameter of the TYPES statement. The data objects STRING and ITAB are declared with data types MYSTRING and MYTAB. The fields of the internal table ITAB are then filled line by line. By using the structure STRING, the contents of ITAB are then displayed on the screen. For further information about internal tables, refer to Creating and Processing Internal Tables.