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
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.
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.
SAP Blog inline declaration