This is a test message to test the length of the message box.
Login
|
Inline decalaration
Created by Software-Heroes

ABAP - Inline declaration (DATA)

2984

Creating data types at runtime and in coding? Now it's no problem with inline declarations.

Advertising


How many programs have you seen so far the first time you have looked into foreign code? You've probably stumbled across many lines of variable definitions at the beginning of every program, form or method. Before the actual business logic began, you have to list all data definitions for all variables.

Fortunately, there were the first major adjustments and the command DATA was once properly revised and implemented ABAP typical.

 

Data definition - OLD

How were variables defined until today, a short example:

We read data from a table, remember the number for later output, and then process the data, remembering the row index, because we may want to remove entries that do not match certain criteria:


" Variable definition in head
DATA:
  lt_address TYPE STANDARD TABLE OF adrc,
  ls_address TYPE adrc,
  ld_tabix   TYPE sytabix,
  ld_count   TYPE i.

" ... Read data

" Save lines
  ld_count = LINES( lt_address ).

" Process
  LOOP AT lt_address INTO ls_address.
    " Save index
    ld_tabix = sy-tabix.

    " ... More logic
  ENDLOOP.

 

If we now implement even more database accesses and processing in this routine, the DATA part will get longer and longer, which makes the whole thing a bit confusing.

 

Data definition - NEW

The definition has now been adapted to the extent that the variable can be defined at the call point. The same example from above, we have now switched to the new world, the determination of the data creates the table, so there is missing the DATA statement. The creation of internal variables in SELECTs is still an additional topic that we will deal with later.


" ... Read data

" Save lines
  DATA(ld_count) = LINES( lt_address ).

" Process
  LOOP AT lt_address INTO DATA(ls_address).
    " Save index
    DATA(ld_tabix) = sy-tabix.

    " ... More logic
  ENDLOOP.

 

All variables are generated at runtime and are determined from the source data type:

  • LINES returns an integer, so ld_count gets the type i
  • within the LOOP ls_address gets the type of the internal table lt_address
  • ld_tabix gets the type of variable from the assignment

 

Usage

After the keyword DATA and FIELD-SYMBOL a bracket is set directly and the variable between them is defined. Unlike previous commands, no spaces are used between parentheses. As you suspected, the whole thing works also well with field symbols, that you can now generate at runtime.


" Loops
LOOP AT lt_address ASSIGNING FIELD-SYMBOL(<ls_address>).
ENDLOOP.

LOOP AT lt_address REFERENCE INTO DATA(lr_address).
ENDLOOP.

" Assigns
ASSIGN COMPONENT 'COUNTRY' OF STRUCTURE ls_address TO FIELD-SYMBOL(<ld_country>).

 

Prerequisite for the processing, however, is the complete typing of the source variable so that the data type can be derived from the context.

 

Validity

The variable is valid from the line of the definition and also usable. Variables that are generated in the LOOP or in an IF statement are also valid outside of this expression. If you want to use the same variable in different loops one after the other, the variable must only be defined in the first loop.

Hint: Here you should be careful when moving logic that your dependent data declaration is still available and in the right place in the code.

 

Source:
SAP Blog inline declaration


Included topics:
New ABAPDATAInline declaration
Comments (0)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Tuesday and Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP - XCO Logging

Category - ABAP

The XCO classes are part of the ABAP Cloud APIs and offer numerous functions that aren't always easy to understand. In this article, we'll take a detailed look at the logging object.

12/16/2025

ABAP - The right Key

Category - ABAP

What about the use of internal tables? Is it still just TYPE TABLE in ABAP, and the table is fully defined?

11/14/2025

ABAP - XCO Regular Expressions

Category - ABAP

Let's take a look at the XCO classes for regular expressions and how you can easily use them to execute REGEX against text and input in ABAP Cloud. We'll also compare them with classic ABAP.

11/07/2025

ABAP - Escape

Category - ABAP

In this article, let's take a closer look at different escape variants that you need for ABAP development and system security.

10/07/2025

ABAP - Date and Time

Category - ABAP

In this article, let's take a closer look at the data types for dates and times in ABAP. Have any changes been made between the various releases, and what should you still use today?

10/03/2025