Application Development 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: 

How to move one column from one internal table to another using VALUE/BASE/FOR?

bmierzwi
Participant
0 Kudos
5,119

Hello,
I have an internal table storing some of the customer's data and I want to append a column from another table which stores e-mail addresses. LIFNR is the common field between tables.
I have the function itself already working:

LOOP AT gt_email_addr INTO gwa_email_addr.
LOOP AT gt_main INTO gwa_main.
IF gwa_main-lifnr = gwa_email_addr-lifnr.
gwa_main-smtp_addr = gwa_email_addr-smtp_addr.
MODIFY gt_main FROM gwa_main.
ENDIF.
ENDLOOP.
ENDLOOP.

BUT I would prefer it to work as a FOR loop. At the moment I am trying something like this:

gt_main = VALUE #( BASE gt_main FOR wa IN gt_email_addr ( CORRESPONDING #( wa ) ) ).
But it doesn't work - the line above actually just appends a new record to gt_main. The documentation seems to be very "tip-of-the-iceberg" about it and provides just some theory and all other sources explain how I can use such a loop to add literal values provided within the code instead and I can't find anything about moving data from one table to another in this way.
Of course, I also tried different versions of that statement, e.g. using WHERE clause (trying to connect those LIFNRs), but, apparently, wa has no such field.

Any ideas what I am doing wrong? It kind of feels like I "just" need to approach the BASE part differently, but nothing seems to work.

Kind Regards,
Bartosz
5 REPLIES 5

Sandra_Rossi
Active Contributor
4,323

You should use Constructor Expressions only to construct values once (construct = "initialize values from scratch"), not construct again just to mimic an update, it will be simply counter-performing.

If you can Construct only once, at a specific place, if you have all data sources available, then you can use VALUE/FOR and any constructor expression you need.

0 Kudos
4,323

Hey,

Thanks for the answer. Do I understand correctly then than my current solution (LOOP within a LOOP) is the correct way to go if I want move data from a single column of an internal table to another internal table?

And that my attempts at optimization should in fact be used only if I want to create a new table?

Kind Regards,

0 Kudos
4,323

rule-of-thumb:

  • To update existing data: don't use Constructor Expressions which rebuild from scratch
  • To initialize a variable the first time: prefer using Constructor Expressions if it's legible

lenapadeken
Product and Topic Expert
Product and Topic Expert
4,323

"The documentation seems to be very "tip-of-the-iceberg" about it and provides just some theory" - really? The documentation provides you with two examples which suit your use case:

https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenfor_in_itab.htm

Best regards,

Lena

0 Kudos
4,323

Hey,

Thank you for the answer and sorry for the late reply.

I've been trying with these, but I really couldn't see how the presented examples fit my use-case. It feels like they're solving a different problem, albeit I suppose this can be attributed to me still being a junior in SAP.

I will analyse it deeper.

Kind Regards,
Bartek