ABAP programming

|  This resource includes primary and/or secondary research. Learn more about original research at Wikiversity.

|

This (greatly undeveloped) article by Dan Polansky intends to provide a glimpse at ABAP syntax and features, where ABAP is a programming language used by SAP enterprise software. To hone ABAP skills, one typically needs to gain access to an SAP system with developer rights; thus, access to ABAP learning environment is not so straightforward as e.g. for the C, C++, Python or Java. The article is rather haphazard and incomplete, which it compensates a bit by the external links/further reading. The most comprehensive online source is probably the official ABAP Keyword Documentation. The Wikipedia article on ABAP currently reads like a guide and is a recommended complementary reading; some of its material may be reusable here.

Gambit Consulting indicates that ABAP syntax resembles the one of COBOL.[1]

ABAP is peculiar by being SAP's proprietary language.[2] Unlike for e.g. Java and C++, there are no multiple implementations of ABAP from different vendors; one can only get ABAP from SAP.

## Tasks suitable for ABAP

[edit | edit source]

Programs in ABAP usually interact with database tables as storage rather than files. Moreover, they can interact with users via relatively plain text output. Also, they can interact with users via screens (also known as "dynpros") featuring input fields, value helps associated with fields, tables, etc.

It seems likely that ABAP is a bit like Java in some regards (especially J2EE) and that it is compiled into something like byte code. However, Java is much more versatile and has been used to create e.g. desktop applications (J2SE was used for that), a task for which ABAP is not suited at all.

## Who uses ABAP

[edit | edit source]

SAP uses ABAP to write their software products, including what used to be SAP R/3 (a follow-on on the mainframe-based R/2), later SAP ERP, and SAP S/4HANA (business suite on the HANA in-memory database from SAP). ABAP is supported by something like ABAP runtime, probably written in C++. The most business application logic in SAP products is probably written in ABAP. Some SAP products or elements of products are probably written in Java. As a result, SAP is probably one of the largest employers of ABAP programmers on the planet. Statista.com indicates the number of SAP employees working in R&D to be over 36 000 as of Feb 2024[3], many of whom are likely to be ABAP developers.

SAP partners and customers use ABAP to write enhancements of the SAP-delivered software. They can also directly modify the SAP-delivered ABAP code, at the cost of greater upgrade pain. Moreover, they can use ABAP to create completely custom applications, using custom data models.

## Hello world

[edit | edit source]

REPORT ZHELLO.
WRITE 'Hello, World!'.

## Variable declaration

[edit | edit source]

Data/variable declaration at a glance:

DATA myint TYPE i.               "Integer
DATA mychar10 TYPE c LENGTH 10.  "Character sequence of length 10
DATA: BEGIN OF complex1,         "Structured data
real TYPE f,             "Floating point
imag TYPE f,             "Floating point
END OF complex1.
DATA complex_nums LIKE TABLE OF complex1.  "Table; complex1 is not a type, so we need "LIKE"
DATA rcomplex1 TYPE REF TO complex1.       "Pointer-like thing called 'reference'.

An issue: should it really be "DATA rcomplex1 TYPE REF TO complex1" rather than "DATA rcomplex1 LIKE REF TO complex1"?

Often, the type of a variable is not defined in terms of elementary built-in types as above but rather is taken from the data dictionary. Types in the data dictionary are trivial to share between ABAP programs; their names are global across ABAP programs.

Instead of TYPE, one can also use LIKE:

DATA myint TYPE i.
DATA myint2 LIKE myint.

Further reading:

* DATA in ABAP Keyword Documentation, help.sap.com

## Type definition

[edit | edit source]

Types can be taken from the data dictionary but they can be also defined in an ABAP program using TYPES keyword.

An example:

TYPES: BEGIN OF complex_type,         "Structured data
real TYPE f,                  "Floating point
imag TYPE f,                  "Floating point
END OF complex_type.
DATA complex_nums TYPE TABLE OF complex_type. "A table of complex numbers represented as a structure

Further reading:

* TYPES in ABAP Keyword Documentation, help.sap.com

## Simple mathematics

[edit | edit source]

Simple mathematics is supported as expected, including +, -, *, /.

There are built-in mathematical functions such as sin, cos, exp, long, etc.

Other mathematical functions, e.g. financial mathematics, are available by calling dedicated function modules and methods.

Further reading:

* Numeric Calculations in ABAP Keyword Documentation, help.sap.com

## Control structures

[edit | edit source]

A glance at IF:

IF ... .
...
ELSEIF ... .
...
ELSE.
...
ENDIF.

A glance at CASE is skipped and delegated to further reading.

A glance at DO (sy-index contains the current index of the loop):

DO 10 TIMES.
WRITE sy-index.
...
ENDDO.

A glance at WHILE:

WHILE ... .
...
ENDWHILE.

There are analogues of C break and continue statements:

* EXIT (like C break): exits the loop, continuing with the first statement after the loop. The keyword has a different role outside of a loop.
* CONTINUE (like C continue): skips toward the end of the current iteration of the loop body.

There is an additional keyword not known from the C language:

* CHECK condition: if the condition is not met, does CONTINUE, that is, skips toward the end of the current iteration of the loop body.

There is a LOOP AT keyword for looping at internal tables, for which see section Internal tables.

Further reading:

* Control Structures in ABAP Keyword Documentation, help.sap.com
* IF in ABAP Keyword Documentation, help.sap.com
* CASE in ABAP Keyword Documentation, help.sap.com
* DO in ABAP Keyword Documentation, help.sap.com
* WHILE in ABAP Keyword Documentation, help.sap.com
* EXIT (loop) in ABAP Keyword Documentation, help.sap.com
* CONTINUE in ABAP Keyword Documentation, help.sap.com
* CHECK in ABAP Keyword Documentation, help.sap.com
* SAP ABAP - Loop Control, tutorialspoint.com
* SAP ABAP - Decisions, tutorialspoint.com

## Internal tables

[edit | edit source]

Internal tables are variables and stand in contrast to database tables; the name "internal" points to that contrast. They are variable-length sequences of rows. The row is usually a fixed field sequence (in C terminology, think "struct"), but it can also be a single field. (In C++ terminology, if the internal table is a "standard table" of a fixed field sequence, one can think of it as a vector of struct.)

There are three types of internal tables:

* Standard tables. They are represented and organized by row index, much like C array or C++ vector.
* Sorted tables.
* Hashed tables.

Looping at internal tables:

LOOP AT gt_flights INTO gs_flight.
...
ENDLOOP.

Looping at internal tables with a modification:

LOOP AT gt_flights INTO gs_flight.
gs_flight-destination = ...
UPDATE gt_flights FROM gs_flight INDEX sy-tabix.
ENDLOOP.

There are more looping options including WHERE, etc.; see the links below.

Internal tables can have header lines, which SAP considers to be an obsolete feature. When they do, one can loop at the table without specifying the variable into which the row is copied; using the table name inside the loop with a hyphen to access a field automatically refers to the header line. For more, see the link below.

Further reading:

* Internal Tables in ABAP Keyword Documentation, help.sap.com
* LOOP AT itab, Basic Form in ABAP Keyword Documentation, help.sap.com
* Internal Tables with a Header Line in ABAP Keyword Documentation, help.sap.com
* SAP ABAP - Internal Tables, tutorialspoint.com
* SAP ABAP - Creating Internal Tables, tutorialspoint.com
* SAP ABAP - Populating Internal Tables, tutorialspoint.com
* 01_Internal_Tables.md, SAP-samples, github.com

## Field symbols

[edit | edit source]

Field symbols are variables/symbols that do not have their own data space reserved. Rather, they point to data space reserved for a normal variable. Their names are like e.g. "<myfieldsymbol>", with the angle brackets mandatory.

They are somewhat like C++ references, but unlike them, field symbols can be unassigned and assigned again. They are somewhat like C pointers, but no dereferencing of a field symbol is required to access its value or data space.

An issue: field symbols can be used to operate dynamically on structures; an exposition is missing.

One can loop at an internal table assigning the current row/line into a field symbol:

FIELD-SYMBOLS <gs_flight> LIKE LINE OF gt_flights.
LOOP AT gt_flights ASSIGNING <gs_flight>.
...
ENDLOOP.

On the other hand, one can equally well use REFERENCE INTO instead of ASSIGNING, doing without field symbols.

Further reading:

* FIELD-SYMBOLS in ABAP Keyword Documentation, help.sap.com
* field symbol (glossary) in ABAP Keyword Documentation, help.sap.com
* ASSIGN in ABAP Keyword Documentation, help.sap.com
* UNASSIGN in ABAP Keyword Documentation, help.sap.com
* IS ASSIGNED in ABAP Keyword Documentation, help.sap.com

## References

[edit | edit source]

What ABAP documentation calls "references" approaches the concept of a pointer from the C language. An ABAP "reference" needs to be dereferenced to access the referent/data referred to.

Declaring references, examples:

DATA ref TYPE REF TO nonclasstype.
DATA ref TYPE REF TO classtype.

To have the reference point to something, one can use GET REFERENCE OF or allocate a new item.

GET REFERENCE OF var INTO ref.
CREATE DATA ref. "Works if the reference type was fully specified; works for non-class types.
CREATE OBJECT ref. "Applies to references to class types.

To access the thing pointed to by a reference:

ref->* "A dereference, e.g. of a reference to an integer
ref->datamember "If the data pointed to by the reference is of a structured type

Links:

* References in ABAP Keyword Documentation, help.sap.com
* Creating Objects and Values in ABAP Keyword Documentation, help.sap.com
* Dereferencing Operator in ABAP Keyword Documentation, help.sap.com
* Object Component Selector in ABAP Keyword Documentation, help.sap.com
* IS BOUND in ABAP Keyword Documentation, help.sap.com
* Assigning Reference Variables in ABAP Keyword Documentation, help.sap.com
* GET REFERENCE in ABAP Keyword Documentation, help.sap.com

## Booleans

[edit | edit source]

ABAP has no built-in boolean data type. By convention, CHAR of length 1 is used as a boolean, where 'X' stands for true and ' ' stands for false.

In newer releases, there is data element ABAP_BOOLEAN defined as CHAR 1; type pool ABAP contains constants abap_true and abap_false. If one wants to use the briefer names "true" and "false", one can roll one's own.

In older code, one may find various data elements used for booleans, including XFELD. What most of these uses have in common is the convention of using CHAR of length 1 and using 'X' for true and ' ' for false; sometimes, '-' is used for undefined/unknown.

In conditions of IF, WHILE, etc., before a certain release, one had to make explicit comparison to truth value, e.g. IF MYSTRUCT-MYFIELD = 'X' instead of IF MYSTRUCT-MYFIELD.

Links:

* DDIC - Predefined Types in ABAP Keyword Documentation, help.sap.com
* Preferred Boolean type for ABAP Development, stackoverflow.com

## Data dictionary

[edit | edit source]

ABAP data dictionary is a repository of type definitions and more. The repository is not stored in includes but rather directly in database tables.

Items in data dictionary:

* Data elements (field types)
* Data dictionary structure types
* Data dictionary table types
* Etc.

Links:

* ABAP - Dictionary (DDIC), in ABAP Keyword Documentation, help.sap.com

## Data element and domain

[edit | edit source]

A data element is part of ABAP data dictionary. It is a field type, to be contrasted to structure types and table types.

The built-in type (e.g. "i" for intefer) for a data element is either specified directly in it. Alternatively, the data element specifies as domain (via its name), from which it inherits the built-in type and other technical properties.

A domain specifies a built-in type, length and, optionally, the number of decimal places. A domain can be used by multiple data elements.

Links:

* DDIC - Data Elements in ABAP Keyword Documentation, help.sap.com
* DDIC - Domains in ABAP Keyword Documentation, help.sap.com

## Comments

[edit | edit source]

There are two kinds of comments: whole-line comments and end-of-line comments. The former kind starts with an asterisk (*) as the first character on the line, no leading whitespace. The latter kind starts with a quotation mark (").

Examples:

*Whole-line comment
A = B + 1. "Comment

Links:

* Comments in ABAP Keyword Documentation, help.sap.com

## Built-in math

[edit | edit source]

Built-in math includes addition, multiplication, etc. but also trigonometric functions, exponentiation, etc. See the link below.

Links:

* Numeric Calculations in ABAP Keyword Documentation, help.sap.com

## Procedures

[edit | edit source]

There are multiple kinds of procedures, including so-called forms (now considered obsolete), function modules and class methods. The names of function modules are global in the sense of being cross-program. For now, see the links below.

Links:

* Calling Procedures in ABAP Keyword Documentation, help.sap.com
* FORM in ABAP Keyword Documentation, help.sap.com
* PERFORM in ABAP Keyword Documentation, help.sap.com
* Function Modules in ABAP Keyword Documentation, help.sap.com
* CALL FUNCTION in ABAP Keyword Documentation, help.sap.com
* METHOD in ABAP Keyword Documentation, help.sap.com
* CALL METHOD in ABAP Keyword Documentation, help.sap.com

## ABAP SQL

[edit | edit source]

ABAP SQL (in older documentation called Open SQL, a term still used by various sources) is a collection of SQL-like statements that can be directly used in ABAP. These statements are not specific to a particular database engine. Unlike in some other programming languages, there is no need to put SQL statements into strings passed to functions/methods or the like; they are first-class citizens of ABAP. More information is currently delegated to further reading.

Links:

* ABAP SQL in ABAP Keyword Documentation, help.sap.com
* SAP ABAP - Open SQL Overview, tutorialspoint.com

## Platform agnosticity

[edit | edit source]

ABAP programs are typically platform agnostic: they run on any operating system, CPU architecture and database engine supported by the ABAP platform (but one would have to clarify whether the in-memory database HANA is fully supported by SAP's Open SQL or is de facto a separate platform.) As a result, they typically do not deal with individual bits of values of variables and make almost no assumption about the value representation as bits and bytes. (Of course, they do deal with there being a maximum integer value, etc.)

Links:

* Excursion Down to Bits and Bytes in ABAP Keyword Documentation, help.sap.com

## Downloading SAP NetWeaver

[edit | edit source]

One can download SAP NetWeaver Application Server ABAP Developer Edition from the link below, to try ABAP.

The trial edition 7.51 SP02 comes in 10 parts, each about 1.4 GB.

Links:

* Developer Trials and Downloads, developers.sap.com

## Selected transactions

[edit | edit source]

Selected transaction codes related to ABAP programming:

* SE11: ABAP Dictionary
* SE16: Data browser
* SE80: ABAP workbench

Links:

* Useful SAP transactions, synactive.com
* List of SAP Transaction codes for ABAP Development, itfinancesite.wordpress.com

## User exits and business add-ins (BADIs)

[edit | edit source]

User exits and BADIs are two different extension mechanisms. User exits are older ones and are procedural; BADIs are based on classes. These extension mechanisms make it possible for the customer to extend the "standard" (SAP-delivered) code without its modification. A disadvantage of modifying the standard code is that once one wants to perform SAP-delivered updates to that code, one has to merge the SAP changes with the custom changes. The two extension mechanisms obviate the need for a merge.

Links:

* What does a user exit mean in SAP? I am just a beginner., quora.com
* SAP ABAP - User Exits, tutorialspoint.com
* SAP ABAP - Business Add-Ins, tutorialspoint.com

## ABAP for SAP GUI

[edit | edit source]

Traditionally, ABAP was used to interact with users via SAP GUI (see Wikipedia). SAP GUI can be thought of as a rich browser and editor tailored for the needs of business software. Most(?) of the business logic does not run in the SAP GUI but rather on the application server, as part of a three-tier architecture (database server -- multiple application servers -- many SAP GUI instances, one per end user). Some logic runs on the database server, e.g. an SQL statement is executed there, and possibly other kinds of executable artifacts.

Nowadays, there are probably other presentation layers available for ABAP, e.g. Fiori[4].

It seems ABAP program written for SAP GUI could run in a web browser, albeit with some limitations. To achieve this, there would be a server and software that on one hand interacts with an application server and on the other hand acts as a web server or interacts with a web server. The name of this technology seems to be "SAP GUI for HTML". A supporting piece of technology seems to be Internet Transaction Server (ITS). It remains to be clarified whether this is of historical interest only or whether SAP still supports or recommends this technology.

SAP GUI seems to be positioned to stay in use for a long time in future, as per Frank Krause from SAP.[5] As per Wikipedia, the most recent SAP GUI version is from 27 Jan 2023, with support until 12 Jan 2027.

Links:

* SAP Graphical User Interface, wikipedia.org
* SAP GUI Family, Additional Topics, help.sap.com
* SAP GUI in ABAP Keyword Documentation, help.sap.com
* user interface in ABAP Keyword Documentation, help.sap.com
* SAP Library - SAP GUI for HTML, help.sap.com

## Program types

[edit | edit source]

ABAP code can be located in different program types. These include "executable program, class pool, function pool, interface pool, module pool, subroutine pool, type pool, and include program."[6] There is also a type called function group; the relationship between function pool and function group is to be clarified.

The ABAP code is typically(?) organized into analogues of files called includes, there generally being multiple includes per program. The include storage is a database one. To get a simplified idea, one can imagine a database table called INCLUDETEXT with fields INCLUDENAME, LINENO, LINETEXT: CHAR120. This is just to give a first idea; the actual implementation is a bit different.

Links:

* program type  in ABAP Keyword Documentation, help.sap.com
* Creating a Function Group, help.sap.com

## Screens or dynpros

[edit | edit source]

ABAP programs can control and interact with screens, also called dynpros, to be shown in SAP GUI. There are special program sections for screen control: process before output (PBO), and process after input (API). See the links below to learn more.

Dynpros are sometimes referred to as classic dynpros, possibly in contrast to Web Dynpro(?).

Links:

* General Dynpros in ABAP Keyword Documentation, help.sap.com
* Dynpro in github.com/SAP-samples
* Wikipedia:de: Dynpro, wikipedia.org (in German)

## Web Dynpro for ABAP

[edit | edit source]

There is something called Web Dynpro. It seems to be a frontend technology different from SAP GUI, an alternative to classic ABAP dynpro interface. The end users seem to be using a web browser.

According to Wikipedia (no tracing to sources): "The earliest version of Web Dynpro appeared in 2003 and was based on Java. This variant was released approximately 18 months before the ABAP variant. As of 2010, the Java variant of Web Dynpro was put into maintenance mode." If this is true, it shows the importance of ABAP as an SAP programming language. For some reason, SAP often refers to the ABAP variant as "Web Dynpro ABAP", without "for" (ABAP).

In 2014, someone wanted to know whether Web Dynpro was obsolete.[7] A very tentative inference from it would be that Web Dynpro is no longer the latest and greatest frontend technology from SAP, unlike Fiori and UI5? (What is the relationship between Fiori and UI5?)

Links:

* Web Dynpro, wikipedia.org
* SAP ABAP - Web Dynpro, tutorialspoint.com
* Web Dynpro in SAP ABAP - Architechture, MVC Model and components, geeksforgeeks.org
* Web Dynpro ABAP, help.sap.com
* Web Dynpro ABAP, Support Content, help.sap.com

## Business Server Pages

[edit | edit source]

Business Server Pages (BSP) seems to be some kind of frontend technology or framework. How exactly that relates to ABAP is not entirely clear from the linked documentation. A best guess is that BSP is a legacy technology, not a shiny new thing. The German Wikipedia page for the concept was created in 2004 and saw almost no update since.

Links:

* Business Server Pages, help.sap.com
* Wikipedia:de: Business Server Pages, wikipedia.org (in German)

## References

[edit | edit source]

1. ↑ SAP ABAP - important terms & functions, GAMBIT Consulting
2. ↑ ABAP Turns 40 | SAP News Center, news.sap.com
3. ↑ SAP number of employees by department 2023 | Statista, statista.com
4. ↑ About ABAP Programming Model for SAP Fiori, help.sap.com
5. ↑ Solved: SAP GUI end of life and transition to SAP Fiori, community.sap.com
6. ↑ program type  in ABAP Keyword Documentation, help.sap.com
7. ↑ Is Web Dynpro obsolete?, community.sap.com

## Further reading

[edit | edit source]

* ABAP, wikipedia.org
* ABAP Keyword Documentation, help.sap.com -- official ABAP documentation by SAP
* SAP ABAP Tutorial, tutorialspoint.com
* topics/abap, github.com -- projects on GitHub pertaining to or written in ABAP
* SAP-samples/abap-cheat-sheets, github.com -- a decent collection of explanatory sheets accompanied by code example files
* Category:ABAP, rosettacode.org
* ABAP 7.40 Quick Reference, 2015, community.sap.com -- ABAP 7.40 enhancements that may confuse ABAP old-timers
* ABAP News for 7.40, SP08 - Open SQL by Horst Keller, 2014, community.sap.com
* SAP ABAP Training Overview, training.sap.com
* Wikipedia:de: SAP ERP (in German) -- a decent overview of some of the SAP products that are programmed in ABAP, much better than the English Wikipedia counterpart
* What is ABAP? A Guide to SAP's Coding Language, learning.sap-press.com -- a worthy read on its own, linking to other worthy reads/blog posts