Initializing Internal Tables

To initialize an internal table with or without a header line, you use the REFRESH statement as follows:

Syntax

REFRESH <itab>.

This statement resets an internal table to the state before it was filled. This means that the table contains no lines.

If you are working with an internal table without table work area, you can use the CLEAR statement instead of the REFRESH statement as follows:

Syntax

CLEAR <itab>.

If you are working with an internal table with a header line, the CLEAR statement clears only the table work area as explained in Resetting Values to Default Values. To reset the whole internal table without clearing the table work area, use either the REFRESH statement or the CLEAR statement as follows:

Syntax

CLEAR <itab>[].

The square brackets after the name of the internal table refer to the body of the internal table.

After using REFRESH or CLEAR to initialize an internal table, the system keeps the space in memory reserved. You can release the memory with the FREE statement as follows:

Syntax

FREE <itab>.

You can also use the FREE statement to reset an internal table and to release its memory directly, without using REFRESH or CLEAR beforehand. Like REFRESH, FREE works on the table body, not on the table work area.

After a FREE statement, you can address the internal table again. The system then reserves memory space again.

You can check whether an internal table is empty by using the following logical expression:

Syntax

... <itab> IS INITIAL ...

example.gif (1342 bytes)

DATA: BEGIN OF LINE,
                    COL1,
                    COL2,
             END OF LINE.
DATA ITAB LIKE LINE OCCURS 10.

LINE-COL1 = 'A'.
LINE-COL2 = 'B'.
APPEND LINE TO ITAB.

REFRESH ITAB.

IF ITAB IS INITIAL.
     WRITE 'ITAB is empty'.
     FREE ITAB.
ENDIF.

The output appears as follows:

ITAB is empty.

In this program, an internal table ITAB is filled and then initialized with REFRESH. In an IF statement, a logical expression with the IS INITIAL parameter is used to check whether ITAB is empty. If so, the memory is released.

Previous
Next


Index



Authored by: Roger Hayen
© Central Michigan Univeristy and/or © SAP America, Inc.
All rights reserved.