SAP ABAP | Data Types (original) (raw)

Last Updated : 11 Oct, 2023

Before Understanding the Data type first understand the Data object. Data objects are variables that we declare in the program. It occupies some memory where you can store the data from external sources. Data can be of different types, so data types are responsible for defining the type of data of the data object. Data types are crucial for memory optimization in the ABAP programming language.

Data-types

Data Types in SAP

Types Of Data Types

There are three types of Data Types

**Elementary Data Types

Elementary data types are single fields. These types are by default in the system kernel. They are categories into two parts:

**Predefined types

There are different predefined types that are by default available in the SAP system kernel.

Here we are providing you the table that specifies the data type, length, initial field lengths and valid field lengths.

Data Type Length Initial Field Length (Bytes) Valid Field Length (Bytes)
C Not Fixed 1 1 to 65535
N not fixed 1 1 to 65535
D fixed 8 characters 8 character
I fixed 4 4
F fixed 8 8
P not fixed 8 8
X not fixed 1 1-65,535
T fixed 6 characters 6 characters
Decfloat16 fixed 8 (16 digits) 8 (16 digits)
Decfloat32 fixed 16 (34 digits) 16 (34 digits)
STRING dynamic Allocated at Run time Allocated at Run time
XSTRING dynamic Allocated at Run time Allocated at Run time

**Syntax:

DATA variable_name TYPE data_type.

**Example: //Declaring zip code
DATA zip_code TYPE c LENGTH 6.
//Initializing ZIP_CODE
ZIP_CODE='201308'.
//PRINT ZIP_CODE
Write ZIP_CODE.

**OUTPUT: 201308

**User Defined Types: User defined type is something that you can define in your program that is know as local user defined or you can define it to centrally in the system that is known as global user define types.

Complex Data Types

Complex data types are combination of different elementary data types. It is similar to struct or record in other programming language by which you can create structured data structure with multiple fields. This helps you in organizing and representing the complex data in an efficient and optimize way.

**Types of complex data types

This data type are classified into two types:

**Structure Data Type: This is a type of complex data type of ABAP programming language that contains or defines a collection of fields each filed has its own data type. Fields data type may be same or different which helps to organize and represent complex data.

**Syntax: Fields are defined using the **BEGIN OF and **END OF statements within the **TYPES declaration.

DATA: BEGIN OF name
fields TYPE field1_data_types,
END OF name

**Example: Here is an example of structure data type

 DATA: BEGIN OF employee  
              empId                 TYPE  n,  
              name                  TYPE c,  
              department       TYPE c,  
              dateOfJoining    TYPE d,  
              END OF  employee

Here we have create a Structure data type named **employee. That contains fields **empID that refers to **Id of an employee that is of type **N(numeric character), name reference to name of the employee of **type C(character), **department reference to department of type C(character) and **dateOfJoining reference to Joining date of employee of **Type D(Date).

**Table Data Types: It is another type of complex data type, that allows you to define a structure for table of data. when there are collections of data in ABAP program then we use this data types. Another name of this data types are array data types because it defines dynamic arrays that can hold multiple data records of the same data type.

**Syntax:

TYPES: BEGIN OF table-name OCCURS initial size,
field_name_1 TYPE data_type_1,
field_name_2 TYPE data_type_2,
...
END OF table-name.

Syntax for using this table data type

 DATA: table-name1 TYPE TABLE OF table-name .

**Example: Here is an example of table data types

TYPES: DATA: BEGIN OF employee
empId TYPE n,
name TYPE c,
department TYPE c,
END OF employee

How to use this table data types

DATA: is_employee TYPE employee,
it_employee TYPE TABLE OF employee

Now add data records

is_employee - empId =01,
is_employee - name = 'John Doe',
ls_employee - department = 'HR'.

Now append data to the internal table it_employee

**APPEND is_employee TO it_employee

Reference Data Types:

Reference data types are the deep data types that describe the reference variable of the data object ,reference variable is reference variable is used to store references or pointers to data objects rather than the actual data itself. This concept is similar to pointers in other programming languages.

**Syntax:

DATA: ref_string TYPE REF TO string,
ref_table TYPE REF TO TABLE OF string.

Reference data types are particularly useful when you need to work with objects dynamically or pass objects by reference, enabling you to manipulate the underlying data or behavior.