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

ABAP - Inline declaration (DATA)

1616

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)



And further ...

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


ABAP - XCO Libraries

Category - ABAP

What can you do with the library in ABAP and ABAP Cloud and how do you use the objects the best way? Find out more here.

11/12/2024

ABAP - Type Casting

Category - ABAP

How do you actually get the original type of a class or instance if it is passed in a generic table? In this article we examine the possibilities.

04/16/2024

ABAP - RETURN value

Category - ABAP

After all these years, the “real” return in ABAP has finally arrived. In this article we will show you how it works and what it can do.

02/13/2024

ABAP Deep Dive - FOR (Loops)

Category - ABAP

Let's take a closer look at the FOR loop. How does it work? What do I have to consider and what can I do with it?

04/14/2023

ABAP Deep Dive - Table access (internal)

Category - ABAP

In this article, let's take a look at table access to internal tables and how they replace READ TABLE.

02/03/2023