Tuesday, November 18, 2008

Saving, Reading, Data Objects & Deleting Data Clusters in Memory,

Saving Data Objects in Memory
To read data objects from an ABAP program into ABAP memory, use the following statement:
Syntax
EXPORT [FROM ] [FROM ] ... TO MEMORY ID .
This statement stores the data objects specified in the list as a cluster in memory. If you do not use the option FROM , the data object is saved under its own name. If you use the FROM option, the data objet is saved under the name . The name identifies the cluster in memory. It may be up to 32 characters long.
The EXPORT statement always completely overwrites the contents of any existing data cluster with the same name .
If you are using internal tables with header lines, you can only store the table itself, not the header line. In the EXPORT statement, the table name is interpreted as the table. This is an exception to the general rule, under which statements normally interpret the table name as a table work area (see Choosing a Table Type).

PROGRAM SAPMZTS1.
DATA TEXT1(10) VALUE 'Exporting'.
DATA ITAB LIKE SBOOK OCCURS 10 WITH HEADER LINE.
DO 5 TIMES.
ITAB-BOOKID = 100 + SY-INDEX.
APPEND ITAB.
ENDDO.
EXPORT TEXT1
TEXT2 FROM 'Literal'
TO MEMORY ID 'text'.
EXPORT ITAB
TO MEMORY ID 'table'.

In this example, the text fields TEXT1 and TEXT2 are stored in the ABAP memory of program SAPMZTS1 under the name "text". The internal table ITAB is stored under the name "table".

Reading Data Objects from Memory
To read data objects from ABAP memory into an ABAP program, use the following statement:

Syntax
IMPORT [TO ] [TO ] ... FROM MEMORY ID .

This statement reads the data objects specified in the list from a cluster in memory. If you do not use the TO option, the data object in memory is assigned to the data object in the program with the same name. If you do use the option, the data object is read from memory into the field . The name identifies the cluster in memory. It may be up to 32 characters long.
You do not have to read all of the objects stored under a particular name . You can restrict the number of objects by specifying their names. If the memory does not contain any objects under the name , SY-SUBRC is set to 4. If, on the other hand, there is a data cluster in memory with the name , SY-SUBRC is always 0, regardless of whether it contained the data object . If the cluster does not contain the data object , the target field remains unchanged.
In this statement, the system does not check whether the structure of the object in memory is compatible with the structure into which you are reading it. The data is transported bit by bit. If the structures are incompatible, the data in the target field may be incorrect.

PROGRAM SAPMZTS1.
DATA TEXT1(10) VALUE 'Exporting'.
DATA ITAB LIKE SBOOK OCCURS 10 WITH HEADER LINE.
DO 5 TIMES.
ITAB-BOOKID = 100 + SY-INDEX.
APPEND ITAB.
ENDDO.
EXPORT TEXT1
TEXT2 FROM 'Literal'
TO MEMORY ID 'text'.
EXPORT ITAB
TO MEMORY ID 'table'.
SUBMIT SAPMZTS2 AND RETURN.
SUBMIT SAPMZTS3.

The first part of this program is the same as the example in the section Saving Data Objects in Memory. In the example, the programs SAPMZTS1 and SAPMZTS2 are called using SUBMIT. You can create and maintain the programs called using the SUBMIT statement by double-clicking their names in the statement. For further information about the SUBMIT statement, refer to Calling Executable Programs (Reports)

Example for SAPMZTS2:
PROGRAM SAPMZTS2.
DATA: TEXT1(10),
TEXT3 LIKE TEXT1 VALUE 'Initial'.
IMPORT TEXT3 FROM MEMORY ID 'text'.
WRITE: / SY-SUBRC, TEXT3.
IMPORT TEXT2 TO TEXT1 FROM MEMORY ID 'text'.
WRITE: / SY-SUBRC, TEXT1.
Example for SAPMZTS3:
PROGRAM SAPMZTS3.
DATA JTAB LIKE SBOOK OCCURS 10 WITH HEADER LINE.
IMPORT ITAB TO JTAB FROM MEMORY ID 'table'.
LOOP AT JTAB.
WRITE / JTAB-BOOKID.
ENDLOOP.

The output is displayed on two successive screens. It looks like this:

The program SAPMZTS2 attempts to read a data object TEXT3 from the data cluster "text", which does not exist. TEXT3 therefore remains unchanged. The existing data object TEXT2 is placed in TEXT1. In both cases, SY-SUBRC is 0, since the cluster "text" contains data.

The program SAPMZTS3 reads the internal table ITAB from the cluster "table" into the internal table JTAB. Both tables have the same structure, namely that of the ABAP Dictionary table SBOOK.

Deleting Data Clusters from Memory
To delete data objects from ABAP memory, use the following statement:
Syntax
FREE MEMORY [ID ].
If you omit the addition ID , the system deletes the entire memory, that is, all of the data clusters previously stored in ABAP memory using EXPORT. If you use the addition ID , this statement only deletes the data cluster with the name .
Only use the FREE MEMORY statement with the ID addition, since deleting the entire memory can also delete the memory contents of system routines.
PROGRAM SAPMZTST.
DATA: TEXT(10) VALUE '0123456789',
IDEN(3) VALUE 'XYZ'.
EXPORT TEXT TO MEMORY ID IDEN.
TEXT = 'xxxxxxxxxx'.
IMPORT TEXT FROM MEMORY ID IDEN.
WRITE: / SY-SUBRC, TEXT.
FREE MEMORY.
TEXT = 'xxxxxxxxxx'.
IMPORT TEXT FROM MEMORY ID IDEN.
WRITE: / SY-SUBRC, TEXT.
This produces the following output:
0 0123456789
4 xxxxxxxxxx
The FREE MEMORY statement deletes the data cluster "XYZ". Consequently, SY-SUBRC is 4 after the following IMPORT statement, and the target field remains unchanged.

No comments: