Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Defaulting a value of a column while using CORRESPONDING#

Former Member
0 Likes
7,299

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 ?

  





  

12 REPLIES 12
Read only

Clemenss
Active Contributor
0 Likes
4,591

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


Read only

Former Member
0 Likes
4,591

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

Read only

raphael_almeida
Active Contributor
0 Likes
4,591

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.

Read only

0 Likes
4,591

Thanks for the information.. however, I seem to be struggling with the syntax.

Read only

0 Likes
4,591

Hi Sheldon.

The REDUCE statement would be as follows:



TYPES: BEGIN OF new_mara_y.
         INCLUDE TYPE mara.
TYPESother_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.

Read only

matt
Active Contributor
0 Likes
4,591

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.

Read only

Former Member
0 Likes
4,591

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 .

Read only

matt
Active Contributor
0 Likes
4,591

Then VALUE and FOR are the way to go.

Something like:

DATA(itab2) = VALUE #( FOR wa IN itab1

                             ( mandt = p_mandt ) ).


See

ABAP News for 7.40, SP08 - FOR Expressions | SCN

Read only

Former Member
0 Likes
4,591

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 ?

Read only

matt
Active Contributor
0 Likes
4,591

Stick with LOOP AT itab ASSIGNING FIELD-SYMBOL( <wa> ) then.

Read only

Clemenss
Active Contributor
0 Likes
4,591

Look at my suggestion without loop

Clemens

Read only

nomssi
Active Contributor
4,591

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