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

ABAP - Inline declaration (DATA)

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

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 bussiness 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 definitionen - 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)

ABAP Deep Dive - VALUE

Category - ABAP

In this article we want to look at the value statement again in all its forms and how you can use it in your daily work.

11/11/2022

ABAP - Common Table Expression (CTE)

Category - ABAP

In this article we want to take a look at the general table expression WITH and how you can use it in everyday life.

10/28/2022

ABAP Deep Dive - CORRESPONDING

Category - ABAP

In this article, a little more about the new Corresponding Statement and how to use it in detail. Let's take a look at the additional features.

09/16/2022

ABAP - Step

Category - ABAP

Today a new addition for the loops and how you can use it. With Step you have the possibility to skip steps in a loop.

09/02/2022

ABAP - Text, String, Template

Category - ABAP

How do I use character strings in modern ABAP development and how do they behave together with inline declarations. We also look at errors that can happen with it.

01/14/2022