≡ Menu

Variable Types in SAP

Variable Types in SAP ABAP: –

ABAP variables or ABAP data objects are containers that carry information and can be worked upon during the course of the ABAP programs by making use of various ABAP statements. The different types of variable types in ABAP are simple variables, structure variables and table variables. We discuss each type in detail.

Simple variables: – A simple variable in ABAP is declared as:

DATA Var1 TYPE I.

Var1 is the variable name and I is the type of the variable that defines it. Here is an integer of elementary data type.

Variables can also be declared in this way:

DATA Var1 TYPE I.

DATA Var2 LIKE Var1

This code first declares Var1 and used Var1 to declare Var2. In this way, several permutations and combinations are available to you while declaring variables in an ABAP program.

Structure variables: – Structure variables are those that are used to declare several different data types. We present the example of a simple SAP ABAP program which clearly illustrates how structure variables are declared and used. Structures are also known as Work Areas.

[codesyntax lang=”abap” title=”structure WA_EMPDET” bookmarkname=”structure WA_EMPDET”]

REPORT  ZVER_PRG1.
**CREATION OF WORKAREA

TYPES : BEGIN OF TY_WA_EMPDET,
        EMPNO TYPE I,
        EMPNAME TYPE STRING,
        EMPSAL TYPE P,
        END   OF TY_WA_EMPDET.


DATA : WA_EMPDET TYPE TY_WA_EMPDET.

*DATA : BEGIN OF WA_EMPDET,
*        EMPNO TYPE I,
*        EMPNAME TYPE STRING,
*        EMPSAL TYPE P,
*       END OF WA_EMPDET.

DATA : WA_EMPDET_DUP LIKE WA_EMPDET.


***INITIALIZATION OF `WORK AREA

WA_EMPDET-EMPNO = 100.
WA_EMPDET-EMPNAME = 'KUMAR'.
WA_EMPDET-EMPSAL = '1000'.

***INITIALIZATION OF DUPLICATE WORK AREA
WA_EMPDET_DUP-EMPNO = 200.
WA_EMPDET_DUP-EMPNAME = 'PRAVEEN'.
WA_EMPDET_DUP-EMPSAL = '2000'.


**** PRINTING THE VALUES FROM WORKAREA

WRITE : 'VALUES FROM ACTUAL WORKAREA',
       /10 'EMPNO=',WA_EMPDET-EMPNO,
       /10 'EMPNAME=',WA_EMPDET-EMPNAME,
       /10 'EMPSAL=',WA_EMPDET-EMPSAL.



SKIP 4.

WRITE : 'VALUES FROM DUPLICATE WORKAREA',
       /10 'EMPNO=',WA_EMPDET_DUP-EMPNO,
       /10 'EMPNAME=',WA_EMPDET_DUP-EMPNAME,
       /10 'EMPSAL=',WA_EMPDET_DUP-EMPSAL

[/codesyntax]

In the above example, we have created a structure WA_EMPDET, in which Integer, String and Packed data types are declared. We have shown how the structure WA_EMPDET is declared, used and manipulated in various ways.

Table variables: – Table variable are used to store internal tables. Internal tables are very similar to structures, except that they may contain multiple records, while a structure has just one record. Internal tables may be compared to arrays in C and C++, however they are different from arrays as they are behaviorally closer to database tables than to arrays, as they represent how data is to be stored in databases. We present a simple ABAP program to show the working of Internal Tables.

[codesyntax lang=”abap” title=”Internal table it_itab ” bookmarkname=”internal table it_itab “]

REPORT  ZITAB_NEW.


TABLES : ZEMPDET.


DATA : IT_ITAB LIKE ZEMPDET OCCURS 0 WITH HEADER LINE.

**PARAMETERS : P_EMPNO LIKE ZEMPDET-ZEMPNO.

SELECT-OPTIONS : S_EMPNO FOR ZEMPDET-ZEMPNO.

INITIALIZATION.
S_EMPNO-SIGN = 'I'.
S_EMPNO-OPTION = 'BT'.
S_EMPNO-LOW = '100'.
S_EMPNO-HIGH = '105'.

APPEND S_EMPNO.


START-OF-SELECTION.



SELECT * FROM ZEMPDET INTO TABLE IT_ITAB WHERE ZEMPNO IN S_EMPNO.







END-OF-SELECTION.


LOOP AT IT_ITAB.

  WRITE :/10 it_itab-zempno,it_itab-zempname,it_itab-zempsal.

  ENDLOOP.

[/codesyntax]

In the above program, an internal table it_itab is declared. Where it differs from the declaration of structures or work areas is in the use of the statements OCCURS and HEADER LINE. These statements clearly mark out it_itab as an internal table rather than as a structure.

The internal table it_itab gets its data from the table ZEMPDET from the database. It is structured in a way that is similar to ZEMPDET.

The following SELECT statement is important because it fills in the data from the database table ZEMPDET into the internal table it_itab by taking s_empno as reference. It_itab has the same data types as ZEMPDET.

SELECT * FROM ZEMPDET INTO TABLE IT_ITAB WHERE ZEMPNO IN S_EMPNO.

Internal tables can also be declared with their own data types, without taking them from an already established table in the database. This example illustrates how it is done. This example also illustrates several new interesting concepts in SAP ABAP such as control-break statements.

[codesyntax lang=”abap” title=”Internal tables ” bookmarkname=”Internal tables “]

DATA : BEGIN OF IT_ITAB OCCURS 0,
        ZEMPNO(10),      "EMPLOYEE NO
        ZEMPNAME(15),    "EMPLOYEE NAME
        MATERIAL(10),    "MATERIAL
        QTY TYPE I,      "QUANTITY
        RATE TYPE P,
        AMT TYPE P,
        END OF IT_ITAB,
        SUBTOT TYPE P,
        GT_TOT TYPE P.



** populate the itab with data

        it_itab-ZEMPNO = '100'.
        it_itab-ZEMPNAME = 'Kumar'.
        it_itab-MATERIAL = 'MAT X'.
        it_itab-QTY = 2.
        it_itab-RATE = 100.
        APPEND it_itab.


        it_itab-ZEMPNO = '101'.
        it_itab-ZEMPNAME = 'prakash'.
        it_itab-MATERIAL = 'MAT Y'.
        it_itab-QTY = 1.
        it_itab-RATE = 100.
        APPEND it_itab.


        it_itab-ZEMPNO = '102'.
        it_itab-ZEMPNAME = 'SATISH'.
        it_itab-MATERIAL = 'MAT X'.
        it_itab-QTY = 2.
        it_itab-RATE = 100.
        APPEND it_itab.

        it_itab-ZEMPNO = '101'.
        it_itab-ZEMPNAME = 'prakash'.
        it_itab-MATERIAL = 'MAT X'.
        it_itab-QTY = 2.
        it_itab-RATE = 100.
        APPEND it_itab.


        it_itab-ZEMPNO = '102'.
        it_itab-ZEMPNAME = 'satish'.
        it_itab-MATERIAL = 'MAT X'.
        it_itab-QTY = 2.
        it_itab-RATE = 100.
        APPEND it_itab.




sort it_itab.
**CALCULATION OF AMT
LOOP AT IT_ITAB.
***calculate the amt
IT_ITAB-AMT = IT_ITAB-QTY * IT_ITAB-RATE.
MODIFY IT_ITAB TRANSPORTING AMT.
ENDLOOP.

SORT IT_ITAB.

LOOP AT IT_ITAB.

AT FIRST.
 WRITE :/10 'EMP No',21 'Emp Name',36 'Material',56 'Qty', 72 'Rate', 85 'Amt'.
 ULINE.
 SKIP.
ENDAT.


  WRITE :/10  it_itab-ZEMPNO,
              it_itab-ZEMPNAME,
              it_itab-MATERIAL,
              it_itab-QTY,
              it_itab-RATE,
              it_itab-AMT.
AT NEW ZEMPNO.

ENDAT.

AT END OF ZEMPNO.
SUM.
SUBTOT = SUBTOT + IT_ITAB-QTY.
SKIP.
WRITE :/31 'SUB TOTAL =',SUBTOT.
ULINE.
SKIP.

**GRAND TOTAL

GT_TOT = GT_TOT + SUBTOT.

CLEAR SUBTOT.
ENDAT.


AT LAST.

WRITE :/29 'Grand TOTAL =',GT_TOT.
skip.
uline.

ENDAT.



  ENDLOOP.

[/codesyntax]