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.

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 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)
Advertising

ABAP - Performance for the SELECT

Category - ABAP

In this article we will look at a few special cases with the SELECT and examine the performance of these constructs. We'll show you the current alternatives and give you little tips while reading.

04/02/2021

ABAP - Check objects (Instances)

Category - ABAP

In this article we will show you how you can analyze instances and react to them correctly, for example if you hand them over during processing and want to react individually.

03/19/2021

ABAP - Loops

Category - ABAP

With New ABAP, new loops and possibilities for restricting table contents were created. We'll take a closer look at these new commands in this article.

02/19/2021

ABAP - Comparison

Category - ABAP

Today we'll look at comparisons and comparison operators in terms of the new commands and their current usage. What has changed so far and what should still be done?

02/12/2021

ABAP - String Functions (Part 3)

Category - ABAP

This article is about other new string functions and a replacement for CONDENSE. We would like to show you why you should use the new functions.

01/22/2021

We use cookies for our services and functions. More informations