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: 

merging of 4 internal table to 1 internal table

naimkhans_babi
Active Participant
0 Kudos

Dear friends,,

can you help me to understand...how i fetch the data from 4 different internal tables to 1 internal table based on one single criterial if sex = m then fetch the data from 4 internal table to one internal table here the partner is the only field which is common in 4 internnal table...shall i use read table...

regards,

naim

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Naim,

LOOP AT ITAB1.

MOVE-CORRESPONDING FIELDS OF ITAB1 TO FINAL_TAB.

READ TABLE ITAB2 WITH KEY XXX = XXX.

MOVE-CORRESPONDING FIELDS OF ITAB2 TO FINAL_TAB.

READ TABLE ITAB3 WITH KEY XXX = XXX.

MOVE-CORRESPONDING FIELDS OF ITAB3 TO FINAL_TAB.

READ TABLE ITAB4 WITH KEY XXX = XXX.

MOVE-CORRESPONDING FIELDS OF ITAB4 TO FINAL_TAB.

APPEND FINAL_TAB.

ENDLOOP.

Now, your FINAL_TAB will have the data of four fields. You should declare FINAL_TAB with all the fields from 4 tables that you want.

Regards,

Ravi

Note - Please mark the helpful answers

8 REPLIES 8

Former Member
0 Kudos

Hi,

You can use inner joins to get the data from four different internal table and populating a single internal table. and the other option is to use select for all entries, read the internal table and move the details correspondingly. When you are trying yo mergr the internal table, there will be a Key field common to all the ITAB which must be used for the comparison.

select a~field1

a~field2

b~field3

b~field4

from table1 as a inner join table2 as b

on afield1 = bfield1

into table itab

where <reqd conditions>.

*field1 = common field.

Note : you final internal table should contain all the fields of the 4 internal tables.

If you are using move-corresponding and read table statements,

loop at itab1.

move-corresponding itab1 to itab.

read table itab2 with key field1 = itab1-field1.

if sy-subrc = 0.

move-corresponding itab2 to itab.

endif.

*then repeat the same statements for the next 2 internal *tables

append itab.

endloop.

Regards,

Aswin

Former Member
0 Kudos

hii

declare one internal table with all the fields

and then modify the final internal table

Loop at itab into wa_itab.

read table itab1 into wa_itab1 index sy-tabix.

wa_itab-field1 = wa_itab1-field1.

  • Do the same for all the fields.

read table itab2 into wa_itab2 index sy-tabix.

wa_itab-field2 = wa_itab2-field2.

  • Do the same for all the fields.

read table itab3 into wa_itab3 index sy-tabix.

wa_itab-field3 = wa_itab3-field3.

  • Do the same for all the fields.

append wa_itab to itab.

Endloop.

same for the fourth too..

Regards

Naresh

0 Kudos

hi

declare an internal table same as ur req...

use nested loop and read the internal table based on ur selection criteria...

plz reward points and close the thread..

regds

gunjan

Former Member
0 Kudos

Naim,

LOOP AT ITAB1.

MOVE-CORRESPONDING FIELDS OF ITAB1 TO FINAL_TAB.

READ TABLE ITAB2 WITH KEY XXX = XXX.

MOVE-CORRESPONDING FIELDS OF ITAB2 TO FINAL_TAB.

READ TABLE ITAB3 WITH KEY XXX = XXX.

MOVE-CORRESPONDING FIELDS OF ITAB3 TO FINAL_TAB.

READ TABLE ITAB4 WITH KEY XXX = XXX.

MOVE-CORRESPONDING FIELDS OF ITAB4 TO FINAL_TAB.

APPEND FINAL_TAB.

ENDLOOP.

Now, your FINAL_TAB will have the data of four fields. You should declare FINAL_TAB with all the fields from 4 tables that you want.

Regards,

Ravi

Note - Please mark the helpful answers

former_member188685
Active Contributor
0 Kudos

Hi,

how are you populating the 4 internal tables ? i mean from which table you are getting the data. can you tell the table names , accordingly we need to decide which one to loop and which one to use read table.

Regards

vijay

Former Member
0 Kudos

Hi Babi

You can fetch the records from all the 4 internal tables into single itab one by one..(declare one temporory internal table )

Thanks

rahulkavuri
Active Contributor
0 Kudos

LOOP AT ITAB.

READ TABLE ITAB2 WITH KEY GENDER = ITAB-GENDER.

IF SY-SUBRC = 0.

READ TABLE ITAB3 WITH KEY gender = ITAB-GENDER.

IF SY-SUBRC = 0.

READ TABLE ITAB4 WITH KEY gender = ITAB3-GENDER.

IF SY-SUBRC = 0.

IT_FINAL-FKIMG = ITAB1-DETAILS.

IT_FINAL-CHARG = ITAB2-DETAILS.

IT_FINAL-NETWR = ITAB3-DETAILS.

ENDIF.

ENDIF.

ENDIF.

ENDLOOP.

former_member927251
Active Contributor
0 Kudos

Hi Naim,

Hope you remember me.

First of all sort all the internal tables by partner to read them using binary search.

Loop at itab1 into wa_itab1.
  Read itab2 into wa_itab2 with key partner = wa_itab1-partner.
  if sy-subrc eq 0.
*     Append the required fields of wa_itab1 to final 
*     internal table
  endif.

*Repeat this for all the internal tables
ENDLOOP.

<b>Reward points if it helps.</b>

Regards,

Amit Mishra