Show Menu
Cheatography

ABAP Programing Cheat Sheet (DRAFT) by

ABAP Programing Cheat Sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Data Types

Define technical properties of all data objects that have these data types, such as the maximum length of a text field.
Are descri­ptions only, with no data memory attached except for admini­str­ative inform­ation.
Can occur in ABAP programs as bound data types, that is, the type is a property of a data object, or as a standalone data type, that is, the data type is defined indepe­nde­ntly.
Can be defined locally in an ABAP program or globally in classes, interfaces and in the ABAP Dictionary (DDIC).
Data types declared in interfaces and in the public visibility section of global classes are also globally visibile. Global classes and interfaces as such are global types to refer to. In classic ABAP, you may stumble on the option to create global data types in type pools, which is not possible in ABAP for Cloud Develo­pment. However, the predefined type pool abap can be used in ABAP for Cloud Develo­pment.

Reference Types

Describe data objects that contain references to other objects (data objects and instances of classes), which are known as reference variables.

There are two kinds of refere­nces: Data references (refer­ences to data objects) and object references (refer­ences to objects as instances of classes).

A reference type must be defined either in the ABAP program or in the ABAP Dictio­nary. There are no built-in reference types in ABAP.

The typical syntax element is ... REF TO ....

Type Conver­sions, Compat­ibility and Assign­ments

A value assignment means that the value of a data object is transf­erred to a target data object. If the data types of the source and target are compat­ible, the content is copied unchanged. If they are incomp­atible and a suitable conversion rule exists, the content is converted. The following cases must be distin­guished with regard to the data type:

The source and target data types are compat­ible, i.e. all technical type properties match. The content is transf­erred from the source to the target without being converted.
The source and target data types are incomp­atible, but can be converted. The content of the source is converted according to the conversion rules and then transf­erred to the target. Two data types are conver­tible if a conversion rule exists for them. An exception is raised if the content of the source cannot be handled according to the conversion rules.
If the data objects are neither compatible nor conver­tible, no assignment can take place. If the syntax check detects this state, a syntax error is raised, otherwise an uncatc­hable exception is raised when the program is executed.

Notes:
The operands of many ABAP statements are assigned internally according to the assignment rules.

Typically, assign­ements are made using the assignment operator =. If necessary and applic­able, the type is converted implic­itly. However, you can also use the conversion operator CONV to convert types explic­itly.

For lossless assign­ments, the lossless operator EXACT can be used to perform checks before the conversion is performed to ensure that only valid values are assigned and that no values are lost in assign­ments.

In general, no checks are performed on assign­ments between compatible data objects. If a data object already contains an invalid value, for example, an invalid date or time in a date or time field, it is passed a valid value when the assignment is made to a compatible data object.

The applie­s_t­o_data method of the RTTI class cl_aba­p_d­ata­descr can be used to check type compat­ibi­lity.

Non-Ad­mis­sible Values of Literals

Note recent syntax warnings when using literals that represent invalid values for target types. The following example demons­trates the assignment of literals using admissible and non-ad­mis­sible values. You can copy and paste the code into a demo class in your SAP BTP ABAP Enviro­nment to explore the syntax warnings.
DATA char3 TYPE c LENGTH 3.

"­Value is admissable and conver­tible
char3 = 'abc'.

"­Non­-ad­mis­sable value assigned to the target (type c length 6)
char3 = 'defghi'.

DATA date TYPE d.

"­Value is admissable and conver­tible
date = '20250­101'.

"­Non­-ad­mis­sable value assigned to the target
"Type i
date = 20250101.
"More characters than type d
date = '20250­101­234'.
"­Fewer characters than type d
date = '202511'.

Creating Structured Types

The type name is preceded by BEGIN OF (which marks the beginning of the structured type defini­tion) and END OF (the end of the defini­tion).
The components - at least one must be defined - are listed in between.
Such structured type defini­tions are usually grouped together in a chained statement, i.e. TYPES is followed by a colon, and the components are separated by commas.
The simplest structures and structured types have elementary compon­ents.
As mentioned above, the components can be of any type, i.e. they can be of structured types themse­lves, internal table types, or reference types.
You can use the TYPE and LIKE additions for the types of the compon­ents. You can use the LINE OF addition to refer to a table type or an internal table.
Outside of classes, you can also refer to DDIC types using LIKE (... comp11 LIKE ddic_type, ...). If you actually want to refer to an existing data object, but due to typing errors you inadve­rtently specify a name that exists as DDIC type, errors may be unavoi­dable.

Demo Example Data Type And Data Object

 

Creating Anonymous Structures

Using the instance operator NEW and CREATE DATA statem­ents, you can create anonymous data objects, such as anonymous struct­ures. The NEW addition of the INTO clause of an ABAP SQL SELECT statement also creates an anonymous data object. As outlined below, you can access the components or the entire data objects by derefe­ren­cing.

Populating Structures

You can copy the content of a structure to another using the assignment operator =. In general, note that special conversion and comparison rules apply to value assign­ments involving struct­ures.
Using the VALUE Operator:The VALUE operator can be used to construct the content of complex data objects such as structures or internal tables.
Using the NEW Operator:Using the instance operator NEW, you can create anonymous data objects, such as anonymous struct­ures. You can access the components or the entire data objects by derefe­ren­cing.
You can use statements with MOVE-C­ORR­ESP­ONDING and the CORRES­PONDING operator to assign values to structure compon­ents, especially when assigning values from a source structure to a target structure which have incomp­atible types and/or differ­ently named compon­ents.
The CL_ABA­P_C­ORR­ESP­ONDING system class is available for making assign­ments.
The INTO clause of ABAP SQL statements has the CORRES­PONDING addition. There, the following basic rule applies, which affects the value assign­ment: Without the CORRES­PONDING ... addition, column names do not matter, only the position. With the CORRES­PONDING ... addition, the position of the columns does not matter, only the name.

Including Structures

INCLUDE TYPE and INCLUDE STRUCTURE statements are used in the context of local struct­ures.
Structured data objects and types created with ... BEGIN OF... END OF ... can use this syntax to include components of another structure, whether it is a locally defined or global structure, without creating substr­uct­ures.
INCLUDE TYPE can be used to include a structured type.
You can use INCLUDE STRUCTURE to include a structure.

They are not additions of ... BEGIN OF ... END OF ... but individual ABAP statem­ents.
If you use a chained statement with a colon to declare the structure, the inclusion of other structures with these statements interrupts the chained statement, that is, the components of the included structures are included as direct components of the supers­tru­cture.
By using the optional AS addition and specifying a name, the included components can be addressed by this common name as if they were actually components of a substr­ucture.
The optional RENAMING WITH SUFFIX addition, followed by a name, gives the included components a suffix name to avoid naming conflicts with other compon­ents.

Demo ABAP Structure

 

Generic Types

Generic types are available with which formal parameters of methods or field symbols can be specified.
At runtime, the actual data type is copied from the assigned actual parameter or memory area, i.e. they receive the complete data type only when an actual parameter is passed or a memory area is assigned.
The TYPE REF TO addition types as a reference variable. A generic type cannot be specified after REF TO. A typing with TYPE REF TO data and TYPE REF TO object is considered as completely typing.

Elementary data types

Elementary (or scalar) data types are based directly on a set of built-in ABAP types.

Numeric types: Integers (b, s, i, int8), decimal floating point numbers (decfl­oat16, decflo­at34), binary floating point numbers (f), and packed numbers (p)
Charac­ter­-like types: text fields (c) and numeric text fields (n)
Byte-like type: byte fields (x)
Charac­ter­-like date and time types: date fields (d) and time fields (t)
Time stamp type for time stamp fields (utclong).
Charac­ter­-like type for text strings (string)
Byte-like type for byte strings (xstring)
The data types c, n, x, and p are incomp­lete, i.e., generic data types, with respect to their length. The type definition syntax has a special addition for this (LENGTH). In addition, p is also generic with respect to the number of decimal places (DECIMALS addition). See more about generic types in the following sections.

Creating Types/Data Objects at Runtime

Using Runtime Type Services (RTTS) you can ...
get type inform­ation on data objects, data types or instances at runtime (Runtime Type Identi­fic­ation (RTTI)).
define and create new data types as type descri­ption objects at runtime (Runtime Type Creation (RTTC)).

ABAP Enumerated Types and Objects

ABAP supports the concept of enumer­ations.
Enumer­ations are a mixture of types and constants.
An enumerated type specifies a value set in addition to the actual type proper­ties.
Enumerated objects - data objects with an enumerated type - are mainly used to check allowed values. This usually restricts the actual parameters passed to methods to the enumerated values defined in the class. Enumerated variables are variable enumerated objects. They can only contain the associated enumerated values.
CDS enumerated types are also available.

Creating Structures

To create a structure (i.e. a structured data object) in an ABAP program, you can use the DATA keyword.
It works in the same way as the TYPES statement above.
Unlike the TYPES statement, you can use the VALUE addition to set default values.
The keywords CLASS-DATA and CONSTANTS can also be used to create struct­ures. In principle, they represent special cases of the general statement shown above.
Structures can also be created inline using DATA(...) or FINAL(...).

Structures

Structures ...
are data objects with structured data types (which is a complex data type because it is composed of other data types).
consist of a sequence of components of any data type, that is, the components of a structure can be, for example, elementary data objects, structures themse­lves, internal tables or refere­nces.
are used to combine different data objects that belong together. A typical example is an address. It has several compon­ents, such as name, street, city, and so on, that belong together.
play an important role in the context of internal tables and database tables. Structured types serve as line types for these tables. Most internal tables across ABAP programs may have structured line types. For database tables, there is no altern­ative to structured line types.
can be created locally in an ABAP program and globally.

Variants of Structures

Flat structures contain only elementary types that have a fixed length, that is, there are no internal tables, reference types or strings as compon­ents. Nesting does not matter in this context. Even a nested structure is considered flat unless a substr­ucture contains a deep component.
DATA: BEGIN OF struc,
comp1 TYPE i,
comp2 TYPE c LENGTH 15,
comp3 TYPE p LENGTH 8 DECIMALS 2,
...,
END OF struc.
Nested struct­ures: At least one component of a structure is a substr­ucture, that is, it refers to another structure. The following example has multiple substr­uct­ures.
DATA: BEGIN OF address_n,
BEGIN OF name,
title   TYPE string VALUE
Mr.
,
prename TYPE string VALUE
Duncan
,
surname TYPE string VALUE
Pea
,
END OF name,
BEGIN OF street,
name TYPE string VALUE
Vegetable Lane
,
num TYPE string VALUE
11
,
END OF street,
BEGIN OF city,
zipcode TYPE string VALUE
349875
,
name  ­  TYPE string VALUE
Botanica
,
END OF city,
END OF address_n.
Deep struct­ures: Contains at least one internal table, reference type, or string as a component.
DATA: BEGIN OF address_d,
name    TYPE string VALUE
Mr. Duncan Pea
,
street  TYPE string VALUE
Vegetable Lane 11
,
city   TYPE string VALUE
349875 Botanica
,
details TYPE TABLE OF some_table WITH EMPTY KEY,
END OF address_d.
The data types of DDIC types are all flat (not nested) struct­ures. Exception: Components of type string can be contained.
Work areas of ABAP SQL statements cannot contain any deep components other than strings among others.
Especially for assign­ments and compar­isons of deep struct­ures, the compat­ibility of the source and target structure must be taken into account.

sy Structure

The sy (or syst) structure is a built-in data object.
The components of the structure represent ABAP system fields.
These fields, filled by the ABAP runtime framework, can be used to query system inform­ation and more.
Typically, they should only be read, and not overwr­itten.
Prominent system fields are the following
 ­sy-­subrc: Return code of many ABAP statem­ents; typically, the value 0 indicates success
 ­sy-­tabix: Row index of internal tables
 ­sy-­index: Loop pass index
These ones and others can be used in ABAP for Cloud Develo­pment. However, most of the fields should not be used in ABAP for Cloud Develo­pment (indicated by a syntax warning) because they refer to Standard ABAP contexts (e.g. classic dynpros and lists), or their values are not relevant in a cloud context.

Clearing Structures

You can reset individual components to their initial values and clear the entire structure using the CLEAR keyword. Note that FREE statements also deletes the content, but they also release the initially allocated memory. space.
 

Complex Data Types

Are composed of other types.

Structured types: Represent a sequence of arbitrary data types (i.e., they can be elemen­tary, reference, or complex data types). The typical syntax element for the local definition of a structure is ... BEGIN OF ... END OF ....

Table types: Consist of a sequence of any number of lines of the same data type. It can be any elementary type, reference type, or complex data type. The type definition includes other properties such as the table category (defines how tables can be accessed) and table key (to identify the table lines). The typical syntax element is ... TABLE OF ....

Enumerated types: Specify a set of values in addition to the actual type proper­ties. The typical syntax element is ... BEGIN OF ENUM ... END OF ENUM ....

Mesh types: Special structured type that contains only table types with structured line types as components that can be linked using mesh associ­ations. The typical syntax element is ... BEGIN OF MESH ... END OF MESH ....

BDEF derived types: RAP-sp­ecific structured and table types. The typical syntax elements are ... TYPE STRUCTURE FOR ... and ... TYPE TABLE FOR .... More inform­ation can be found here and in the ABAP cheat sheet on ABAP EML.

Data objects

Are objects (or instances) of a data type (similar to object­s/i­nst­ances of classes in ABAP Objects).
Occupy memory space and exist in different forms, for example, numeric or textual data can be contained in data objects.
The type of data that a data object can receive is determined by its data type.
Like data types, their existence and visibility depend on the declar­ation context.
Are usually used in ABAP statements by specifying them in the operand position.

Ranges Tables

Internal tables that have the predefined columns SIGN, OPTION, LOW, and HIGH
Declared with the TYPE RANGE OF addition in DATA and TYPES statements
Used to store range conditions that can be evaluated in expres­sions using the IN operator (each row in the table represents a separate compar­ison)

Typed Literals in ABAP SQL

Literal whose data types is defined by specifying a built-in dictionary type explic­itly.
Available for most but not all ABAP Dictionary data types.
Can be used in ABAP SQL and in ABAP CDS.
Advantages of typed literals over untyped literals
Allow type-safe use of literals
Eliminate the need for (implicit type) conver­sions and casts, which can lead to surprising or erroneous results; also consider the conversion costs in terms of perfor­mance (typed literals are passed to the database and evaluated there without ABAP-s­pecific type conver­sions).
For better readab­ility (you can immedi­ately see what type is being used)

Globally Available Structures and Structured Types

Apart from the local declar­ation of a structured type, you can create such a type, for example, as global DDIC structure in the ABAP Dictio­nary. Such a DDIC structure defines a globally available structured type (DDIC type).

There are other structured types available globally, which may be the structured types most commonly used in ABAP programs:

Database tables defined in the ABAP Dictionary can be used as data types just like DDIC structures in an ABAP program. This means that when you create a structure in your ABAP program, for example, you can simply use the name of a database table to address the line type of the table. The structure you created will then have the same structured type as the database table. Typically, you use the database tables to create structures of such a type, or internal tables of such a structured line type, to process data read from the database table in structures or internal tables.

Various CDS entities are globally available structured types. For example, a CDS view entity represents a structured data type and can be used as such in ABAP programs (but not in the ABAP Dictionary).

Structures and structured data types can be defined in the public visibility section of global classes or in global interfaces and then used globally.

Creating Structures by Inline Declar­ation

This is partic­ularly useful for declaring data objects at the operand positions where you actually need them.
In this way, you can avoid an extra declar­ation of the structure in different contexts.
You can use the declar­ation operator using DATA(...). The FINAL declar­ation operator is used to create immutable variables.
You can also create structures using the VALUE operator (and also fill them as shown below). Without specifying component values in the parent­heses, you create an initial structure.

Accessing (Compo­nents of) Structures

Structures can be accessed as a whole. You can also address the individual components of structures at the approp­riate operand positions.
To address the compon­ents, use the structure component selector -.
For variables with reference to a structured data object, the object component selector -> can be used: ...dre­f->comp .... The following syntax also works, but is less conven­ient: ... dref->­*-comp ....
ADT and the ABAP Editor provide code completion for structure components after the component selectors.

Processing Structures

Boxed Components

In struct­ures, boxed components represent nested structures managed by an internal reference.
Currently, static boxes are supported as boxed compon­ents, enabling initial value sharing. Find more inform­ation here.
When used:
 ­Opt­imize memory consum­ption for structures used repeat­edly, such as in internal tables with nested struct­ures. Without boxed compon­ents, memory increases line by line, even if the nested structure is initial. With boxed compon­ents, memory does not increase when nested structures are initial, and only reads are performed.
 ­Enhance runtime perfor­mance since assign­ments for components with active initial value sharing require only the internal reference, not additional data to be copied.
Boxed components allocate memory when there is write access to at least one component or when a field symbol is assigned or data reference points to at least one component.