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

Merging two internal tables

aaruljothi
Participant
0 Likes
4,253

Hi,

I have two internal tables it_tab1 and it_tab2. The fields are,

it_tab1 it_tab2

-


-


F1 F1

F2 F2

F3 F4

F6 F5

I want to <b>merge</b>(coloumn wise) these two internal tables into a new internal table it_tab3 where it_tab3 has fields,

F1, F2, F3, F4, F5, F6.(All the fields)

Here, in it_tab1 and in it_tab2 the fields <b>F1 and F2 are the key fields</b>.The number of records in the two internal tables are different.

Can anybody help me. thanx for spending ur time.

1 ACCEPTED SOLUTION
Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,654

Hi,

Here is the sample code.

Loop at itab1 into wa1.

read table itab2 into wa2 with key f1 = wa1-f1

f2 = wa1-fa2.

if sy-subrc eq 0.

move-corresponding wa1 to wa3.

move-corresponding wa2 to wa3.

append wa3 to itab3.

endif.

endloop.

Kindly reward points by clicking the star on the left of reply,if it is useful.Get back if you need clarifications.

9 REPLIES 9
Read only

Former Member
0 Likes
1,654

Identify which tab has the maximum no of records.

if itab1 has the maximum then

identify the no of records using describe stmt

create the structure of itab3 as required.

loop at itab1.

loop at itab2 wher f1 = itab1- f1 and f2 = itab1-f2.

move the field values to itab3 .

append itab3 .

endloop.

endloop.

Read only

Former Member
0 Likes
1,654

Hi ..

If the no of records in both the tables are different then do we need to update only the common records in both the table to the final table?

With Regards,

Ranganthan

Read only

Former Member
0 Likes
1,654

...

Message was edited by: Winnie Chan

Read only

Former Member
0 Likes
1,654

Hi,

SInce u have mentioned the key fields are present in both the internal tables.

You can -

declare it_itab3 with all the fields.

sort it_itab2 by F1 F2.

Loop at t_itab1.

it_itab3-f1 = t_itab1-F1.

it_itab3-f2 = t_itab1-F2.

it_itab3-f3 = t_itab1-F3.

it_itab3-f6 = t_itab1-F6.

read table t_itab2 with key F1 = t_itab1-F1

F2 = t_itab1-F2

binary search.

if sy-subrc eq 0.

it_itab3-f4 = t_itab2-f4.

it_itab3-f5 = t_itab2-f5.

append it_itab3.

endif.

Endloop.

if u want the records even if the corresponding values of F1, F2 of itab1 is not there in iTAB2 then use append outside the if statement.

Read only

0 Likes
1,654

Sharath has the right answer. Avoid nesting a loop at where clause inside another loop. It can get slow very easily.

The only thing to add would be to only read table2 when the keys changed.

Read only

Former Member
0 Likes
1,654

Hi try similar to this code.

DATA: BEGIN OF itab1 OCCURS 0,
       no TYPE c,
     END OF itab1.

DATA: BEGIN OF itab2 OCCURS 0,
       no TYPE c,
     END OF itab2.
DATA: BEGIN OF itab3 OCCURS 0,
       no TYPE c,
     END OF itab3.

itab1-no = 'A'.
APPEND itab1.
itab1-no = 'B'.
APPEND itab1.
itab1-no = 'C'.
APPEND itab1.
itab1-no = 'D'.
APPEND itab1.
itab1-no = 'E'.
APPEND itab1.

itab2-no = 'A'.
APPEND itab2.
itab2-no = 'B'.
APPEND itab2.
itab2-no = 'D'.
APPEND itab2.
itab2-no = 'F'.
APPEND itab2.
itab2-no = 'H'.
APPEND itab2.

SORT itab1.
SORT itab2.

clear itab1.
clear itab2.
clear itab3.
itab3[] =  itab1[].
APPEND itab3.
clear itab3.

INSERT LINES OF itab2 INTO TABLE itab3.

SORT itab3.
DELETE ADJACENT DUPLICATES FROM itab3.

Hope this helps.

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,655

Hi,

Here is the sample code.

Loop at itab1 into wa1.

read table itab2 into wa2 with key f1 = wa1-f1

f2 = wa1-fa2.

if sy-subrc eq 0.

move-corresponding wa1 to wa3.

move-corresponding wa2 to wa3.

append wa3 to itab3.

endif.

endloop.

Kindly reward points by clicking the star on the left of reply,if it is useful.Get back if you need clarifications.

Read only

Former Member
0 Likes
1,654

hi,

try this

hi,

itab1 - first

itab2 - second

itab3 - both

data l_count type i.

l_count = 1.

loop at itab1.

read table itab2 with key f1 = itab1-f1 f2 = itab1-f2.

itab3 = itab1

...

if sy-subrc is initial

itab3-field6 = itab2-field3.

itab3-field7 = itab2-field4.

append itab3.

endif.

endloop.

loop at itab2.

itab3 = itab2

read table itab1 with key f1 = itab2-f1 f2 = itab2-f2.

if sy-subrc is initial

itab3-field6 = itab1-field3.

itab3-field7 = itab1-field4.

append itab3.

endif.

endloop.

sort itab3 by f1 f2.

delete adjacent duplicates by comparing f1 f2.

hope this will solve your problem

cheers,

sasi

Cheers,

Sasi

Read only

Former Member
0 Likes
1,654

Little alteration to what Bala Muthu Raja replied.

Use this logic so that you also capture the records from itab1 for which there is no entry in itab2.

loop at itab1.

loop at itab2 wher f1 = itab1- f1 and f2 = itab1-f2.

move the fields from itab2 to itab3 .

endloop.

Move fields from itab1 to itab 3

append itab3 .

endloop.