Variables are named data objects used to store values within the allotted memory area of a program. As the name suggests, users can change the content of variables with the help of ABAP statements. Each variable in ABAP has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used. The basic form of a variable declaration is −
DATA <f> TYPE <type> VALUE <val>.
Here <f> specifies the name of a variable. The name of the variable can be up to 30 characters long. <type> specifies the type of variable. Any data type with fully specified technical attributes is known as <type>. The <val> specifies the initial value of the of <f> variable. In case you define an elementary fixed-length variable, the DATA statement automatically populates the value of the variable with the type-specific initial value. Other possible values for <val> can be a literal, constant, or an explicit clause, such as Is INITIAL.
Following are valid examples of variable declarations.
DATA d1(2) TYPE C.
DATA d2 LIKE d1.
DATA minimum_value TYPE I VALUE 10.
In the above code snippet, d1 is a variable of C type, d2 is a variable of d1 type, and minimum_value is a variable of ABAP integer type I.
This chapter will explain various variable types available in ABAP. There are three kinds of variables in ABAP −
- Static Variables
- Reference Variables
- System Variables
Static Variables
- Static variables are declared in subroutines, function modules, and static methods.
- The lifetime is linked to the context of the declaration.
- With ‘CLASS-DATA’ statement, you can declare variables within the classes.
- The ‘PARAMETERS’ statement can be used to declare the elementary data objects that are linked to input fields on a selection screen.
- You can also declare the internal tables that are linked to input fields on a selection screen by using ‘SELECT-OPTIONS’ statement.
Following are the conventions used while naming a variable −
- You cannot use special characters such as "t" and "," to name variables.
- The name of the predefined data objects can’t be changed.
- The name of the variable can’t be the same as any ABAP keyword or clause.
- The name of the variables must convey the meaning of the variable without the need for further comments.
- Hyphens are reserved to represent the components of structures. Therefore, you are supposed to avoid hyphens in variable names.
- The underscore character can be used to separate compound words.
This program shows how to declare a variable using the PARAMETERS statement −
REPORT ZTest123_01.
PARAMETERS: NAME(10) TYPE C,
CLASS TYPE I,
SCORE TYPE P DECIMALS 2,
CONNECT TYPE MARA-MATNR.
Here, NAME represents a parameter of 10 characters, CLASS specifies a parameter of integer type with the default size in bytes, SCORE represents a packed type parameter with values up to two decimal places, and CONNECT refers to the MARA-MATNF type of ABAP Dictionary.
The above code produces the following output −
Reference Variables
The syntax for declaring reference variables is −
DATA <ref> TYPE REF TO <type> VALUE IS INITIAL.
- REF TO addition declares a reference variable ref.
- The specification after REF TO specifies the static type of the reference variable.
- The static type restricts the set of objects to which <ref> can refer.
- The dynamic type of reference variable is the data type or class to which it currently refers.
- The static type is always more general or the same as the dynamic type.
- The TYPE addition is used to create a bound reference type and as a start value, and only IS INITIAL can be specified after the VALUE addition.
Example
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA Bl TYPE I VALUE 1.
ENDCLASS. DATA: Oref TYPE REF TO C1 ,
Dref1 LIKE REF TO Oref,
Dref2 TYPE REF TO I .
CREATE OBJECT Oref.
GET REFERENCE OF Oref INTO Dref1.
CREATE DATA Dref2.
Dref2→* = Dref1→*→Bl.
- In the above code snippet, an object reference Oref and two data reference variables Dref1 and Dref2 are declared.
- Both data reference variables are fully typed and can be dereferenced using the dereferencing operator →* at operand positions.
System Variables
- ABAP system variables are accessible from all ABAP programs.
- These fields are actually filled by the run-time environment.
- The values in these fields indicate the state of the system at any given point of time.
- You can find the complete list of system variables in the SYST table in SAP.
- Individual fields of the SYST structure can be accessed by using either “SYST-” or “SY-”.
Example
REPORT Z_Test123_01.
WRITE:/'SY-ABCDE', SY-ABCDE,
/'SY-DATUM', SY-DATUM,
/'SY-DBSYS', SY-DBSYS,
/'SY-HOST ', SY-HOST,
/'SY-LANGU', SY-LANGU,
/'SY-MANDT', SY-MANDT,
/'SY-OPSYS', SY-OPSYS,
/'SY-SAPRL', SY-SAPRL,
/'SY-SYSID', SY-SYSID,
/'SY-TCODE', SY-TCODE,
/'SY-UNAME', SY-UNAME,
/'SY-UZEIT', SY-UZEIT..
The above code produces the following output −
SY-ABCDE ABCDEFGHIJKLMNOPQRSTUVWXYZ
SY-DATUM 12.09.2015
SY-DBSYS ORACLE
SY-HOST sapserver
SY-LANGU EN
SY-MANDT 800
SY-OPSYS Windows NT
SY-SAPRL 700
SY-SYSID DMO
SY-TCODE SE38
SY-UNAME SAPUSER
SY-UZEIT 14:25:48
No comments:
Post a Comment