2016 Jun 11 6:30 AM
Hi Experts,
I am new to the new ABAP syntax and need to perform the following operation.
Requirement : it_item and et_item are tables which a slightly different.
a) it_item has ( key ....other fields )
b) et_item has ( mandt, db_key ... other fields)
I need to move contents from it_item to et_item while mapping the key from it_item into db_key of et_item.
At the same time, I need to fill the mandt field with a fixed value.
Old syntax :
In terms of performance this is slower than the new syntax.
LOOP AT it_item ASSIGNING <ls_item>.
APPEND INITIAL LINE TO et_item ASSIGNING <ls_item_db>.
MOVE-CORRESPONDING <ls_item> TO <ls_item_db>.
<ls_item_db>-db_key = <ls_item>-key.
<ls_item_db>-mandt = is_client-mandt.
ENDLOOP.
I replaced the old syntax with the new ABAP constructs.
New syntax:
et_item = CORRESPONDING #( it_chrg_item MAPPING db_key = key ).
This works fine to map all fields except it cannot fill the mandt field.
Is it possible to fill et_item with the mandt field simultaneously while using the new syntax ?
OR
Is it possible to fill all contents of et_item with mandt with the new ABAP constructs , I do not want to loop over all et_item contents ?
2016 Jun 11 3:36 PM
Hi Sheldon Rodrigues,
you canset a single value without a loop since ever (?) using the statement
MODIFY itab - itab_lines
data ls_item like line of it_item.
lt_item-mandt = sy-mandt.
modify it_item from ls_item transporting mandt where mandt <> ls_item-mandt.
Using new syntax it could be simplified further - sorry I can't try.
Regards Clemens
2016 Jun 12 7:27 AM
I just did a quick performance check the modify is slower than loo at assigning <field-symbol> ?
So I would go with Loop at assigning <field-symbol> ..worst case
2016 Jun 11 9:47 PM
Hi Sheldon,
If you are using version 7.4 SP >= 08 you can use the REDUCE to mount such an new internal table. Check the documentation for more information, oks?!
Warm regards,
Raphael Pacheco.
2016 Jun 12 7:29 AM
Thanks for the information.. however, I seem to be struggling with the syntax.
2016 Jun 12 2:10 PM
Hi Sheldon.
The REDUCE statement would be as follows:
TYPES: BEGIN OF new_mara_y.
INCLUDE TYPE mara.
TYPES: other_field TYPE c.
TYPES: END OF new_mara_y.
TYPES new_mara_t TYPE STANDARD TABLE OF new_mara_y WITH DEFAULT KEY.
SELECT * FROM mara INTO TABLE @DATA(mara_t) UP TO 10 ROWS.
DATA(new_mara) = REDUCE new_mara_t( INIT out = VALUE new_mara_t( )
FOR i IN mara_t
NEXT out = VALUE #( BASE out ( VALUE new_mara_y( BASE CORRESPONDING new_mara_y( i )
mandt = sy-mandt
other_field = abap_true ) ) ) ).
cl_demo_output=>display( new_mara ).
Where "new_mara" is the structure that receives the mounting data from the new_mara_t type and the result of mara_t (Using CORRESPONDING). As my query already had the MANDT, I did not put it there, I put to you see how is the CORRESPONDING + mapping fields.
Warm regards,
Raphael Pacheco.
2016 Jun 12 7:23 AM
Why do you want to fill the MANDT field? Unless you are using CLIENT SPECIFIED, you don't need to. And you should only use CLIENT SPECIFIED if you are deliberately writing a cross-client application - which in nearly 20 years I've never actually seen.
2016 Jun 12 7:26 AM
I am writing a migration report which has to be client independent.. so I need to fill the mandt field based on the new structure details .
2016 Jun 12 7:43 AM
Then VALUE and FOR are the way to go.
Something like:
DATA(itab2) = VALUE #( FOR wa IN itab1
( mandt = p_mandt ) ).
See
2016 Jun 12 8:10 AM
Hi Matthew,
Thanks for the inputs so far...
As mentioned in my main post I mapped it_chrg_item to et_item using ...
et_item = CORRESPONDING #( it_chrg_item MAPPING db_key = key ).
As mentioned by you to fill the mandt field I used
et_item = VALUE #( FOR <ls_item> IN et_item ( mandt = p_mandt ) ).
However now all the other field in et_item are overwritten and only mandt field seems to be filled.
I need all the fields in et_item including mandt to be filled.
Additionally, is there a way of combining CORRESPONDING# and VALUE# into one construct ?
2016 Jun 12 12:28 PM
Stick with LOOP AT itab ASSIGNING FIELD-SYMBOL( <wa> ) then.
2016 Jun 12 2:06 PM
2016 Jun 12 6:11 PM
on newer releases, you can combine VALUE and CORRESPONDING using BASE, e.g.
VALUE ts_target( BASE CORRESPONDING #( ls_origin )
mandt = sy-mandt
uname = sy-uname ).
JNN