Inline declaration ABAP - DATA()

It is important to know about inline declarations:

  • Inline variables are defined only locally
  • The type of the variable is determined at the time of compilation,
    and not at the time of creation of the variable
  • A variable defined inline within a construct, such as LOOP or CASE, remains available outside this construct.

Inline declaration in ABAP has been available since version 7.4, in previous ones old recording options were used. There are many options for using inline declorination, but they will not replace the completely ordinary form of declaring variables, but will only make it possible in some cases to replace it with a new type of record.

Creating a variable at any time

+ Conveniently  when creating a variable, immediately fill it with the necessary data and it is ready for use without extra lines.

- The disadvantage some indicate a decrease in code readability, but in my opinion this is a moot point.

" Create a structure
DATA(ls_scar) = VALUE scarr( carrid   = 'AA'
                             carrname = 'American Air' ).
" Create a table
DATA(lt_scar) = VALUE scarr_t( ( carrid   = 'AA'
                              	 carrname = 'American Air' ) ).

Reading data into an internal table

+ Conveniently  read data from a database table into an internal table defined inline. In this case, the program itself will form a table with the columns specified after the SELECT operator. At the same time, we do not need to pre-determine the type of the internal table or read extra data into it using the existing type.

- The disadvantage This option is that the table that is defined inline is always the standard (STANDART TABLE) . And working with standard tables with a large amount of data is not the best option. It is more optimal to use sorted or hashed tables, depending on the specific case.

SELECT carrid, carrname
  INTO TABLE @DATA(lt_scarr)
  FROM scarr
  UP TO 5 ROWS.	

Getting a row or link to a row in an internal table

+ Conveniently without defining additional variables, go through the internal table and, for example, change the values in the rows. Or read the data using the READ TABLE construct and place the link to the string in a variable defined inline.

- The disadvantage This option is called the fact that if one variable is used twice in the same cycles, it will not work to swap them without additional code changes. Also, I heard comments that this version of the definition reduces the readability of the code. In my opinion, this option is convenient and I do not see any real minuses.

LOOP AT lt_scarr REFERENCE INTO DATA(lr_scarr). "Get the link
ENDLOOP.
	
LOOP AT lt_scarr INTO DATA(ls_scarr). "Get a copy of the string
ENDLOOP.	

"Similarly, the time limit is determined for READ TABLE
READ TABLE lt_data REFERENCE INTO DATA(lr_data).

When calling methods in the input and output parameters

+ Conveniently at the time of the method call, for example, define a table or structure and immediately pass it to the method, without even creating a separate variable with the name. For example,
table "VALUE #( ( day = '22' year = '2020' ) )" or
the structure "VALUE #( day = '22' year = '2020' )", filling in only the fields necessary for us.
Also, we can directly call the method to put the result of its execution in a variable defined inline.

- Limitations of this method is that we cannot pass an inline variable to a method or get it directly, when called, if the parameter has a generic type DATA ( RV_RESULT Returning Type Ref To DATA ). Also, we cannot, for obvious reasons, define variables CHANGING inline. An important limitation is that we cannot use inline declaration directly when calling FM.

cl_salv_table=>factory( 
EXPORTING t_table = VALUE #( ( day = '22' year = 2020 ) )
IMPORTING r_salv_table = DATA(lr_grid) ).

" FM restrictions
DATA:
  lv_new_day TYPE SCAL-INDICATOR.

CALL FUNCTION 'DATE_COMPUTE_DAY'
  EXPORTING
    DATE          = CONV sy-datum( |20200322| )
  IMPORTING
    DAY           = lv_new_day.

" Thus, it is impossible to obtain a parameter from FM
CALL FUNCTION 'DATE_COMPUTE_DAY'
  EXPORTING
    DATE          = sy-datum
  IMPORTING
    DAY           = DATA(lv_day).

This article does not fully cover the entire broad topic of inline declaration, so the second part of this article will be released shortly.