ABAP Data Types
ABAP (Advanced Business Application Programming) provides a variety of data types that are used to define variables and constants in programs. Understanding data types is crucial for writing efficient and effective ABAP code.
Elementary Data Types
Numeric Data Types
I
(Integer): Used for whole numbers.DATA: lv_number TYPE I.
F
(Floating-point number): Used for floating-point numbers.DATA: lv_float TYPE F.
P
(Packed number/Decimal): Used for decimal numbers with a specified number of decimal places.DATA: lv_amount TYPE P DECIMALS 2.
Character Data Types
C
(Character): Used for fixed-length character strings.DATA: lv_char TYPE C LENGTH 10.
N
(Numeric text): Used for numeric text strings (digits only).DATA: lv_numtext TYPE N LENGTH 5.
D
(Date): Used for date values in the format YYYYMMDD.DATA: lv_date TYPE D.
T
(Time): Used for time values in the format HHMMSS.DATA: lv_time TYPE T.
STRING
: Used for variable-length character strings.DATA: lv_string TYPE STRING.
Byte Data Types
X
(Hexadecimal): Used for hexadecimal values.DATA: lv_hex TYPE X LENGTH 4.
XSTRING
: Used for variable-length hexadecimal strings.DATA: lv_xstring TYPE XSTRING.
Complex Data Types
Structures
-
A structure is a grouping of fields, each of which can have a different data type.
TYPES: BEGIN OF ty_structure, field1 TYPE I, field2 TYPE C LENGTH 10, END OF ty_structure. DATA: ls_structure TYPE ty_structure.
Internal Tables
-
Internal tables are used to store collections of data in rows, similar to arrays.
TYPES: BEGIN OF ty_table, field1 TYPE I, field2 TYPE C LENGTH 10, END OF ty_table. DATA: lt_table TYPE TABLE OF ty_table.
References and Object Types
REF TO
: Used for object references.DATA: lo_object TYPE REF TO cl_example_class.
Constants
- Constants in ABAP are variables whose value cannot be changed once defined.
CONSTANTS: lc_pi TYPE P DECIMALS 2 VALUE '3.14'.
Example Usage
REPORT z_data_types_example.
DATA: lv_int TYPE I VALUE 100,
lv_char TYPE C LENGTH 5 VALUE 'ABAP',
lv_date TYPE D VALUE '20230101',
lv_amount TYPE P DECIMALS 2 VALUE '1234.56'.
WRITE: / 'Integer:', lv_int,
/ 'Character:', lv_char,
/ 'Date:', lv_date,
/ 'Amount:', lv_amount.
Comprehensive ABAP Types with Hungarian Notation
Integer
- Type: Integer
- Hungarian Notation:
i_
- Example:
i_counter
Character/String
- Type: Character/String
- Hungarian Notation:
c_
- Example:
c_name
Packed Number (Decimal)
- Type: Packed Number (Decimal)
- Hungarian Notation:
p_
- Example:
p_amount
Floating-point Number
- Type: Floating-point Number
- Hungarian Notation:
f_
- Example:
f_value
Date
- Type: Date
- Hungarian Notation:
d_
- Example:
d_start_date
Time
- Type: Time
- Hungarian Notation:
t_
- Example:
t_end_time
Boolean/Logical
- Type: Boolean/Logical
- Hungarian Notation:
l_
- Example:
l_flag
Reference
- Type: Reference
- Hungarian Notation:
r_
orlo_
- Example:
r_pointer
,lo_employee
Local Variable
- Type: Local Variable
- Hungarian Notation:
lv_
- Example:
lv_temp
Global Variable
- Type: Global Variable
- Hungarian Notation:
gv_
- Example:
gv_total
Global Internal Table
- Type: Global Internal Table
- Hungarian Notation:
gt_
- Example:
gt_data
Local Internal Table
- Type: Local Internal Table
- Hungarian Notation:
lt_
- Example:
lt_employees
Global Structure
- Type: Global Structure
- Hungarian Notation:
gs_
- Example:
gs_employee
Local Structure
- Type: Local Structure
- Hungarian Notation:
ls_
- Example:
ls_record
Type Table
- Type: Type Table
- Hungarian Notation:
tt_
- Example:
tt_employee_table
Type Definition
- Type: Type Definition
- Hungarian Notation:
ty_
- Example:
ty_employee
Class Name
- Type: Class Name
- Hungarian Notation:
cl_
- Example:
cl_employee
Interface Name
- Type: Interface Name
- Hungarian Notation:
if_
- Example:
if_employee_handler
Constant
- Type: Constant
- Hungarian Notation:
co_
- Example:
co_max_value
Member Object
- Type: Member Object
- Hungarian Notation:
mo_
- Example:
mo_employee
Member Table
- Type: Member Table
- Hungarian Notation:
mt_
- Example:
mt_employee_list
Member Structure
- Type: Member Structure
- Hungarian Notation:
ms_
- Example:
ms_employee_data
System Variables
- Type: System Variables (not a prefix, but commonly used)
- Hungarian Notation:
sy_
- Example:
sy-datum
(current date)
Type | Hungarian Notation | Example | Description |
---|---|---|---|
Integer | i_ | i_counter | An integer is a whole number without a decimal point. Used for counters, iterations, etc. |
Character/String | c_ | c_name | A character or string is a sequence of characters. Used for names, descriptions, etc. |
Packed Number (Decimal) | p_ | p_amount | A packed number is a decimal number stored in a compact format. Used for monetary values, quantities, etc. |
Floating-point Number | f_ | f_value | A floating-point number represents a real number with fractional parts. Used for values requiring precision, like scientific measurements. |
Date | d_ | d_start_date | A date is a point in time typically represented as day, month, and year. Used for date fields. |
Time | t_ | t_end_time | A time represents a specific time of day, usually as hours, minutes, and seconds. Used for time fields. |
Boolean/Logical | l_ | l_flag | A boolean or logical type represents truth values: true or false. Used for flags, switches, etc. |
Reference | r_ or lo_ | r_pointer, lo_employee | A reference or object pointer to another object or data type. Used for referencing objects in ABAP. |
Local Variable | lv_ | lv_temp | A local variable is a variable declared within a function or method, accessible only within that scope. |
Global Variable | gv_ | gv_total | A global variable is accessible throughout the entire program or module, regardless of scope. |
Global Internal Table | gt_ | gt_data | A global internal table is a table that can be accessed globally across the program. |
Local Internal Table | lt_ | lt_employees | A local internal table is a temporary table used within a local scope, such as within a method or function. |
Global Structure | gs_ | gs_employee | A global structure is a complex data type that can hold multiple fields of different data types, accessible globally. |
Local Structure | ls_ | ls_record | A local structure is a complex data type with multiple fields of different data types, accessible locally. |
Type Table | tt_ | tt_employee_table | A type table is a predefined type that defines the structure of internal tables. |
Type Definition | ty_ | ty_employee | A type definition creates a custom type used for defining variables or structures. |
Class Name | cl_ | cl_employee | A class name is the name of a class, which is a blueprint for creating objects in ABAP. |
Interface Name | if_ | if_employee_handler | An interface name defines a set of methods that a class must implement. |
Constant | co_ | co_max_value | A constant is a value that cannot be changed during the program execution. |
Member Object | mo_ | mo_employee | A member object is an instance of a class, representing a specific realization of the class. |
Member Table | mt_ | mt_employee_list | A member table is an internal table that is a member of a class, used to store multiple entries. |
Member Structure | ms_ | ms_employee_data | A member structure is a structure that is a member of a class, used to group related data fields. |
System Variables | sy_ | sy-datum | System variables are predefined variables provided by the SAP system, often used for system-related information. |