ABAP - Corresponding and Value
The two expressions focus primarily on creating structures and moving data content in the context of tables. We want to show you today the two statements and which commands they replace.
Table of contents
Today we want to bring you the two commands Corresponding and Value a little bit closer. As in the last article, the terms are so-called constructor expressions, if you do not know what that means, then you can read this in our last article.
Corresponding
The new command replaces the Move-Corresponding, as you have probably already thought. A new type can be defined directly at the transfer. The command has also some defined additions that will make your life a lot easier.
Here's an example of how easy the new command works:
" Easy Move
DATA(ls_t001) = CORRESPONDING t001( ls_data ).
For a simple move no complex use is necessary, here the command can simply be written down "flat".
It looks different, if you want to merge several structures, here you need an addition, otherwise the previously mapped values will be deleted from your target structure. This is the so-called base for moving the data. In the following example, we combine three structures into one target structure:
" Move with Base
ls_data = CORRESPONDING #( ls_t001 ).
ls_data = CORRESPONDING #( BASE( ls_data ) ls_bkpf ).
ls_data = CORRESPONDING #( BASE( ls_data ) ls_skb1 ).
The addition transfers the already filled fields from ls_data and then adds them to the fields of the additional structure, in this case company code and G/L account.
Another, very interesting, addition is the mapping for fields that do not fit by name. Previously you had to use a Move-Corresponding and then map all deviant fields. This can now be done after the "Mapping" command.
" Move with Mapping
ls_data = CORRESPONDING #( ls_t001 MAPPING company_code = bukrs description = butxt ).
The assignment is in principle "target field = source field from the structure". Here, any number of fields can be added, which should be additionally mapped.
Value
The "Value" command is used primarily to create structures and tables. The use of inserts, updates or appends is very flexible, saves space in the coding and above all, it saves initialization effort, because the structure is only generated by the Value statement. Between the parentheses of the Value the assignment of the required fields takes place. You just have to fill in the fields you need, the rest will be filled up with initial values.
Here are some examples for the command:
" Fill a Range
DATA lt_bukrs TYPE RANGE OF t001-bukrs.
APPEND VALUE #( sign = 'I' option = 'EQ' low = 'A001' ) TO lt_bukrs.
APPEND VALUE #( sign = 'I' option = 'EQ' low = 'A002' ) TO lt_bukrs.
APPEND VALUE #( sign = 'I' option = 'EQ' low = 'A003' ) TO lt_bukrs.
" Create a structure
DATA(ls_t001) = VALUE t001( bukrs = 'A001' butxt = 'Value-Test' land1 = 'EN' ).
" Create table
TYPES tt_data TYPE STANDARD TABLE OF char25 WITH EMPTY KEY.
DATA(lt_data) = VALUE tt_data( ( '123' ) ( '456' ) ( '789' ) ).
Hint: Important when defining the table and using a self-defined table type is the definition of the key. In this case, we have defined an "empty key" to have a key. Without adding the key definition, the code can not be generated and the error message "A value of the generic type TT_DATA can not be constructed."
Conclusion
Today's featured functions again provide a lot of flexibility in development and shorten the effort of writing in some places. We hope you like the two new keywords as well as we do and we hope you enjoy using them.
Source:
SAP Documentation Corresponding
SAP Documentation Value