[SAP ABAP] Data declaration

After learning about the objects in the Data Dictionary, we can start talking about the syntax declaration in ABAP. The easiest way to declare variables is as follows:
data: lv_first_variable TYPE c. 
Variables are named data objects that are used to store values within the allotted memory area of a program. It is good practice to properly name local variables. It is assumed that the first two characters in the variable name determine its use. The first letter indicates whether the variable is global or local. If the local variable starts with l, then in the case of global variables, g is the first letter. The second character determines what type the object is:
  • Variablev
  • Lines
  • Internal tablet
Each variable must have a type that describes what is stored inside. A variable may have the following types:
  • Elementary types
  • References
  • User-defined types
  • Generic ABAP types
In the ABAP language, you can use predefined types to define local data types and objects in a program and to specify the type of interface parameters and field symbols. These are divided into numeric types, character types, and hexadecimal types. These types can be defined globally in the ABAP Data Dictionary or locally in an ABAP program. Some data types are already defined in the ABAP Data Dictionary. Variables are also divided by type. The first type is static variables, which are declared in subroutines, function modules, and static methods. 
The elementary types are mentioned in ABAP. They can be used directly to define a variable. User-created types are also based on elementary ones. The SAP system has the following elementary types:
  • Type C: Its initial size is 1, and the range of characters it can accept is 1-65535. The initial value is space. It accepts alphanumeric characters. It's used to process text.
  • Type N: This type of variable contains strings of digits. It is like the C type variable since it has an initial size of 1 and a maximum of 65535. Its initial value is 0, and it is used to express identifying numbers.
  •  Type D: This is an eight-character variable. The type used for the date. The initial value is 00000000.
  •  Type T: This is a six-character variable. The type used for the time value. The initial value is 000000.
  •  Type I: This is used to store integers, and the initial value is 0. The range of values is +231 to -231.
  • Type F: This is a floating-point number, where the accuracy range is approximately 15 decimals. The range of values is 10307 to -10307.
  •  Type P: Numbers are stored in compressed formats; they are a maximum of 31 digits.
  •  Type STRING: Like type C, there is no defined length.
  •  Type XSTRING:  Like type X, there is no defined length.
For the CNDI, and F variables, the programmer can define a length. If they don't, the variables will take the minimum number of characters. The user can also specify their initial values, other than standard values. An example initialization of a variable that has 3 characters with the initial value, 'ABC', is shown in the following code:
data: lv_first_variable(3) TYPE c VALUE 'ABC'. 
As we can see, the number of characters is written after the variable name in brackets. Adding the word VALUE at the end of the initialization adds the initial value.

Field symbol

field symbol is an instrument in which applications are created with elasticity. Field symbols do not have any memory; instead, they will be pointing to a memory location. An element can be compared to a pointer from other programming languages.The following code will help you to declare field symbols using different typing methods:
 TYPES: BEGIN OF ts_type,
          first_data  TYPE string,
          second_data TYPE i,
          third_data  TYPE c,
        END OF tt_type.

 DATA : ls_type TYPE tt_type,
        lt_type TYPE STANDARD TABLE OF  tt_type.

 FIELD-SYMBOLS: <fs_type1> TYPE c,
                <fs_type2> TYPE tt_type,
                <fs_type4> TYPE data,
                <fs_type5> TYPE any,
                <fs_type6> TYPE ANY TABLE,
                <fs_type7> LIKE ls_type,
                <fs_type8> LIKE LINE OF lt_type.
In order to use a field symbol, we must assign it to the object value. This results in the o element not reserving a physical data field space:
ASSIGN lv_value TO <fs_value>.
Field symbols can also be used to reference internal table variables, as follows: 
TYPES: BEGIN OF ts_type,
          first_data  TYPE string,
          second_data TYPE i,
          third_data  TYPE c,
        END OF ts_type.

 DATA : lt_type TYPE STANDARD TABLE OF  ts_type.

 LOOP AT lt_type ASSIGNING FIELD-SYMBOL(<fs>).
   <fs>-first_data = 'First_data'.
   <fs>-second_data = 2.
   <fs>-third_data = 'X'.
 ENDLOOP.
The presented method of using the symbol field is one possible way of doing this. The declaration takes place dynamically in this case. You can also use the previously declared variable. An example of this implementation is as follows:
TYPES: BEGIN OF ts_type,
          first_data  TYPE string,
          second_data TYPE i,
          third_data  TYPE c,
END OF ts_type.
DATA : lt_type TYPE STANDARD TABLE OF ts_type.

FIELD-SYMBOLS: <fs> TYPE ts_type.

LOOP AT lt_type ASSIGNING <fs>.
   <fs>-first_data = 'First_data'.
   <fs>-second_data = 2.
   <fs>-third_data = 'X'.
ENDLOOP.

Comments

Popular posts from this blog

[SAP ABAP] BAPI

[SAP HR] Payroll Schema