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_ or lo_
  • 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)
TypeHungarian NotationExampleDescription
Integeri_i_counterAn integer is a whole number without a decimal point. Used for counters, iterations, etc.
Character/Stringc_c_nameA character or string is a sequence of characters. Used for names, descriptions, etc.
Packed Number (Decimal)p_p_amountA packed number is a decimal number stored in a compact format. Used for monetary values, quantities, etc.
Floating-point Numberf_f_valueA floating-point number represents a real number with fractional parts. Used for values requiring precision, like scientific measurements.
Dated_d_start_dateA date is a point in time typically represented as day, month, and year. Used for date fields.
Timet_t_end_timeA time represents a specific time of day, usually as hours, minutes, and seconds. Used for time fields.
Boolean/Logicall_l_flagA boolean or logical type represents truth values: true or false. Used for flags, switches, etc.
Referencer_ or lo_r_pointer, lo_employeeA reference or object pointer to another object or data type. Used for referencing objects in ABAP.
Local Variablelv_lv_tempA local variable is a variable declared within a function or method, accessible only within that scope.
Global Variablegv_gv_totalA global variable is accessible throughout the entire program or module, regardless of scope.
Global Internal Tablegt_gt_dataA global internal table is a table that can be accessed globally across the program.
Local Internal Tablelt_lt_employeesA local internal table is a temporary table used within a local scope, such as within a method or function.
Global Structuregs_gs_employeeA global structure is a complex data type that can hold multiple fields of different data types, accessible globally.
Local Structurels_ls_recordA local structure is a complex data type with multiple fields of different data types, accessible locally.
Type Tablett_tt_employee_tableA type table is a predefined type that defines the structure of internal tables.
Type Definitionty_ty_employeeA type definition creates a custom type used for defining variables or structures.
Class Namecl_cl_employeeA class name is the name of a class, which is a blueprint for creating objects in ABAP.
Interface Nameif_if_employee_handlerAn interface name defines a set of methods that a class must implement.
Constantco_co_max_valueA constant is a value that cannot be changed during the program execution.
Member Objectmo_mo_employeeA member object is an instance of a class, representing a specific realization of the class.
Member Tablemt_mt_employee_listA member table is an internal table that is a member of a class, used to store multiple entries.
Member Structurems_ms_employee_dataA member structure is a structure that is a member of a class, used to group related data fields.
System Variablessy_sy-datumSystem variables are predefined variables provided by the SAP system, often used for system-related information.