Complex data types are created with the combination of elementary types. ABAP supports complex data types. Complex types allow us to manage and process conceptually-related data under a single name. Complex types can be processed as a whole or individually.
Complex data types are further divided into two types and those are –
- Structure data types
- Array type or Internal table data types
Structure Data Types –
Structure types are used to group the elements that logically belong together. The elements of structure can be a combination of any data type or same data type.
Syntax –
DATA: BEGIN OF {structure-name}
{local variables declaration}
END OF {structure-name}.
Structures are classified as four types based on their definition and those are –
Structure Type | Description |
---|---|
Simple Structures | Contains elementary datatypes of fixed/variable length as elements and called as non-nested structures. |
Nested structures | Contains one or more structures elements within a structure element. |
Flat structures | Contains only elementary datatypes of fixed length. |
Deep structures | Contains at least one internal table, reference type, string as component. |
Example –
Write a simple program to get the understanding of Structure variable.
Code –
*&---------------------------------------------------------------------*
*& Report Z_STRUCTURE_VARIABLE
*&---------------------------------------------------------------------*
*& Program Written by TUTORIALSCAMPUS
*&---------------------------------------------------------------------*
REPORT Z_STRUCTURE_VARIABLE.
* Declaring student structure with student no, student name and
* student class
DATA: BEGIN OF student,
no TYPE n,
name(25) TYPE c,
class(10) TYPE c,
END OF student.
* Assigning value to student no
MOVE 1 TO student-no.
* Assigning value to student name
MOVE 'pawan' TO student-name.
* Assigning value to student class
MOVE '1st class' TO student-class.
* Displaying student structure details by using stucture appending
WRITE : 'student-no :', student-no,
/ 'student-name :', student-name,
/ 'student-class:', student-class.
Output –
Explaining Example –
student is a structure variable. no, name and class are elementary variables in student structure variable.
The structure variables are referring like – no as student-no, name as student-name and class as student-class.
These references student-no, student-name and student-class are used in programming process to manuplate the data.
Internal Table or Array Types –
Internal table contains series of lines which are repeated from single line. One line may contain single or multiple elements that are combination of same or different data types. Internal table has a line data type used to identify the table rows using unique or non-unique key. Table type determines how the table entries can be accessed. Internal table most advanced version of array and also called as array.
Syntax –
TYPES: BEGIN OF {table-name}
{local variables declaration}
END OF {table-name}.
DATA: {table-name1} TYPE {table-name} OCCURS n.
Example –
Simple example to create product information internal table with below structure.
Code –
REPORT Z_INTERNAL_TABLE.
* internal table Structure creation
TYPES: BEGIN OF t_product,
pid(10) TYPE C,
pname(40) TYPE C,
pamount(10) TYPE P,
END OF t_product.
* Data & internal table declaration
DATA: wa TYPE t_product,
wa1 TYPE t_product,
it TYPE TABLE OF t_product.
wa-pid = 'IFB1'.
wa-pname = 'IFB WASHING MACHINE'.
wa-pamount = 31000.
* Appending data to the internal table
APPEND wa TO it.
* Reading internal table of index 1
READ TABLE it INTO wa1 INDEX 1.
IF sy-subrc = 0.
WRITE: wa1-pid, wa1-pname, wa1-pamount.
ELSE.
WRITE 'No Record Found'.
ENDIF.
Output –
Explaining Example –
TYPES: BEGIN OF t_product…. | Structure declaration with pid(product id), pname(product name) and pamount(product amount) |
---|---|
DATA: wa TYPE t_product | Declaring Work area(wa) of type t_product |
wa1 TYPE t_product | Declaring Work area1(wa1) of type t_product |
it TYPE TABLE OF t_product | Internal table declaration of type t_product |
wa-pid = ‘IFB1’ | Assigning value to pid(product id) |
wa-pname = ‘IFB WASHING MACHINE’ | Assigning value to pname(product name) |
wa-pamount = 31000 | Assigning value to pamount(product name) |
APPEND wa TO it | Assigning work area(wa) data to the table(it) data |
READ TABLE it INTO wa1 INDEX 1 | Reading internal table(it) with index 1 and assign it to work area1(wa1) |
WRITE: wa1-pid, wa1-pname, wa1-pamount | Displaying the work area1 (wa1) using structure fields |