The SAP® ABAP programming language provides its developers the power to create dynamic and flexible applications. In a previous article in ERPtips titled Saving Customer Time and Budgets via Flexible Programming (June 2004), we introduced some of the techniques that will let you do flexible and dynamic programming and avoid hard-coding values in programs. One important dynamic programming technique used by SAP developers entails a combination of field symbols and data references. In this article, we will see how this is done.
We will start with a brief explanation of Data References and their advantages and uses. The structure of programs that use field symbols and data references will also be discussed. Finally I will present a fully functional program that allows you to read a database table name, and then uses dynamic programming concepts to print its contents. (The user enters the table name and the program prints its contents).
Please note that the field symbols and references concept is vast and a lot of variants exist. However, in this article, we discuss the important forms that may be used in dynamic programming. For a detailed discussion of field symbols and/or data references, refer to the SAP documentation at http://help.sap.com.
These are some of the questions that this article will address:
What are the commands and statements for creating data references in programs?
How are field symbols declared in programs?
What are the steps to create data objects dynamically using reference variables?
How do I combine field symbols and data references for creating dynamic programs?
This article is primarily intended for ABAP developers and consultants. I will assume that the reader is familiar with basic ABAP concepts and has some knowledge of dynamic programming. All the screen images in this article have been taken from ECC 6.0, although the instructions are relevant to other versions of SAP as well.
An Overview of ABAP Data Reference Variables and Data References
Every data object declared within your program has an address in the memory. The address is also known as the reference. SAP ABAP lets you define data reference variables within the program. These variables contain the address (or reference) of data objects, and may be said as pointing to the object in question. A data reference variable may point to single fields, as well as to structures and internal tables.
You may simply define a data reference variable as shown below:
DATA: MYREF TYPE REF TO TYPENAME.
The data reference variable may point to data objects of any type. For example, you may define a data reference that points to a date. The declaration will be like the one shown below:
DATA: MYREF_D TYPE REF TO D.
In the above case, the type that the declaration points to has been fully specified (i.e., date). You may also make a generic declaration. A generic declaration will look like the one shown below:
DATA: MYGENREF TYPE REF TO DATA.
In case this, the type is not specified, but rather we used a generic type, DATA. This may point to a data object of any type. While creating data objects, you need to specify what type of data object the generic reference is to point to (we will see this in the latter part of the article).
When a data reference variable is initially defined, it points to no data object (i.e., it has a NULL reference). If you want to instantiate the data references variable from data variables, you must use a GET REFERENCE statement, as shown below:
GET REFERENCE OF MY_DATE INTO MYREF.
The above statement will populate the reference variable MYREF with the address of the data object MY_DATE.
In order to access the content of the object to which a data reference is pointing, the reference must be de-referenced. You may use the dereferencing operator (->*) for this purpose.
For example, you may have a reference MYREF pointing to a date; by using the dereferencing operator, we can write MYREF->* and the value of the date that is pointed to is printed, as follows:
WRITE: MYREF->*.
You may also use the MOVE statement in order to assign a value to the location the reference is pointing to. An example of this is shown below:
MOVE ’20100101′ to MYREF->*.
After the above statement is run, the MYREF reference variable will point to the date having a value of 01.01.2010.
You may assign the value of a reference variable to another reference variable, such as
MYREF = MYREF2.
Generation of ABAP Data Objects
You may use data references in conjunction with the CREATE DATA statement in order to generate data objects at runtime. You may use the CREATE DATA statement to create single fields structures as well as internal tables.
The generic syntax of this is as follows:
DATA: MYREF TYPE REF TO TYPENAME.
CREATE DATA MYREF.
A simple example for creating a date at runtime is shown below (the “D” indicates date):
DATA : MYREF TYPE REF TO D.
CREATE DATA MYREF.
The above would create a data object having a date type and the MYREF would be a reference to it.
If you declared a generic type reference variable, then you need to specify the type of the data object to be created in the CREATE DATA statement. The generic syntax form of this is shown below:
DATA: MYREF TYPE REF TO DATA.
CREATE DATA MYREF TYPE TYPENAME.
Our date example may be changed as shown below:
DATA : MYREF TYPE REF TO DATA.
CREATE DATA MYREF TYPE D
No comments:
Post a Comment