Open In App

SAP ABAP | Data Types

Last Updated : 11 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 Types
  • Complex Types
  • Reference 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
  • User defined types.

Predefined types

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

  • C : C refers to characters whenever we have to process characters characters we use C data types. By default its size is 1 character but you can additionally specify the length using ‘LENGTH’ keyword.
  • N : N refers to numeric character, if we want to store numbers we use N data types. You can additionally specify the length using ‘LENGTH’ keyword. You can’t perform arithmetic operation on this type of data object.
  • I : I refers to integer, it is used in storing I integer value. It’s size is fixed and can not specify extra length. It’s size is also fixed and you can perform arithmetic operation on this type data object.
  • D : This data type is use when we have to declare date. It’s size is fixed and when we declare D the system will create a data object whose size is of 8 characters.
  • T : This data type is for Time. It’s size is fixed and system will create a data object whose size is of 6 character.
  • F : F stands for floating point. This data type is use when we have to store very large number. It’s size is also fixed and you can perform arithmetic operation on this type data object.
  • P : P stands for Packed decimal . It is use to store integer with up to n decimal points. You can specify length of P.
  • Decfloat16 : It is decimal floating point of size 8 bytes that can store up to 16 digits it is combination of both integer and floating point.
  • Decfloat34 : This is the another version of decimal floating point of size 16 bytes that can store up to 34 digits.
  • X : This is used for hexadecimal values and for storing binary data.
  • STRING : This data type can store alphanumeric character, it is of dynamic length that means memory allocation done at run time. This helps in memory optimization.
  • XSTRING : It’s purpose is same as of X data type both is used for hexadecimal values but XSTRING is of dynamic length. It is commonly used for handling binary files such as images.

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 type
  • Table type.

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads