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

Moving data from one internal table to another

Former Member
0 Likes
1,128

Hi,

I have one internal table populated with a list of material codes and another with all the material numbers.

ITAB1: MCODE KSCHL KNUMH MATNR

ITAB2: MCODE MATNR

The matnr field in itab1 is blank initially. It needs to be populated with all the material numbers from itab2. How can I achieve this?

Thanks,

ap

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,093

Try this code

Loop at itab1 into wa_itab1

readt table itab2 into wa_itab2 with key mcode = wa_itab1-mcode.

IF sy-subrc eq 0.

wa_itab1-matnr = wa_itab2-matnr

ENDIF.

modify itab1 from wa_itab1.

endloop

Hi,

I have one internal table populated with a list of material codes and another with all the material numbers.

ITAB1: MCODE KSCHL KNUMH MATNR

ITAB2: MCODE MATNR

The matnr field in itab1 is blank initially. It needs to be populated with all the material numbers from itab2. How can I achieve this?

Thanks,

ap

10 REPLIES 10
Read only

Former Member
0 Likes
1,094

Try this code

Loop at itab1 into wa_itab1

readt table itab2 into wa_itab2 with key mcode = wa_itab1-mcode.

IF sy-subrc eq 0.

wa_itab1-matnr = wa_itab2-matnr

ENDIF.

modify itab1 from wa_itab1.

endloop

Read only

Former Member
0 Likes
1,093

In both tables, is there a one to one relationship between MCODE and MATNR?

Rob

Read only

Former Member
0 Likes
1,093

Hi,

Please try this.


DATA: WA_ITAB2 LIKE ITAB2.

LOOP AT ITAB1.
   READ TABLE ITAB2 INTO WA_ITAB2
                    WITH KEY MCODE = ITAB1-MCODE.
   
  IF SY-SUBRC = 0.                               
   MODIFY ITAB1 FROM WA_ITAB2 TRANSPORTING MATNR.
  ENDIF.
ENDLOOP.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
1,093

data: v_tabix like sy-tabix.

sort itab1 by mcode.

sort itab2 by mcode.

loop at itab1.

v_tabix = sy-tabix.

read table itab2 with key mcode = itab1-mcode.

if sy-subrc = 0.

itab1-matnr = itab2-matnr.

modify table itab1 index v_tabix.

endif.

clear: itab1, itab2, v_tabix.

endloop.

Read only

Former Member
0 Likes
1,093

HI,

Loop at Itab2.
read table itab1 with key MCODE = itab2-MCODE.
if sy-subrc = 0.
 itab2-Matnr = itab1-matnr.
modify Itab2 index sy-tabix.
endif.
endloop.

Regards

Sudheer

Read only

Former Member
0 Likes
1,093

Hi,

The above solutions are for a one to one relationship. The material code will have many material numbers under it. e.g. If the material in itab1 is ABC, it will have 10 material numbers in itab2. So the one record in itab1 should become 10. I hope this is clear.

a.p.

Read only

0 Likes
1,093

The best way to get this done is use a inner join statement.

thanks,

Read only

0 Likes
1,093

Hi

Lets say that there is only one record in itab1 and for that there is correspondingly 10 records in itab2.

that means that the one record in itab1 should be changed to the first material number in itab2 and there should be nine more records created in itab1 according to the values in itab2.

Hope this is what you mean.

If this is the logic then the following code should work

then lets have a third internal table the same type as itab1

Loop at itab1 into wa_itab1

*readt table itab2 into wa_itab2 with key mcode = wa_itab1-mcode.

*IF sy-subrc eq 0.

loop at itab2 into wa_itab2 where mcode = wa_itab1-mcode.

wa_itab1-matnr = wa_itab2-matnr

append wa_itab1 to itab3.

endloop.

*ENDIF.

*modify itab1 from wa_itab1.

endloop.

itab3 will contain all the values that you require

Read only

Former Member
0 Likes
1,093

try this,

SELECT tab1~matnr

tab2~mcode

tab1~kschl

tab1~knumh

INTO TABLE ittab

FROM tab1

INNER JOIN tab2

ON tab1mcode EQ tab2mcode

WHERE (your condition).

Read only

Former Member
0 Likes
1,093

Thanks Dominic! And congratulations on your 1000 points milestone!