cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP: Merge two internal tables

former_member773564
Discoverer
0 Kudos

Hi,

I tried to merge two internal tables (itab_1 & 2) into another one (itab_3). They have the same type.

Here the coding:

LOOP AT ITAB_2.
READ TABLE ITAB_1 WITH KEY: PERNR = ITAB_2-PERNR, DAT2 = ITAB_2-DAT1.
IF SY-SUBRC = 0.

ITAB_3-PERNR = ITAB_1-PERNR.
ITAB_3-DAT1 = ITAB_1-DA1.
ITAB_3-DAT2 = ITAB_2-DAT2.
* ITAB_3-DAT3 = ITAB_1-DAT3.
ITAB_3-NAME = ITAB_2-NAME.
ITAB_3-STATUS = ITAB_1-STATUS.
ENDIF.
AT LAST.
CONTINUE.
ENDAT.
APPEND ITAB_3.
ENDLOOP.

Result

The problem is that, when the conditions are verified at a line, the next one will be filled with new data of itab_2, but previous data of itab_1, as long as the PERNR is the same.

I would like to merge this like an inner join

How can I improve my coding, so that I can merge the two internal tables with matching entries?

I thank you in advance!

manfred_reinart
Advisor
Advisor

Self contained example with sample data filled would be great 😉

You did not state how you'd like to merge the two tables - base don which criterieon... If this should rather work like an inner or outer join. Therefore some smaple tables with content and desired output would be a good idea.

Sandra_Rossi
Active Contributor
0 Kudos

Please edit your question (Actions>Edit), select your code and press the button [CODE], which makes the code appear colored/indented, it'll be easier for people to look at it. Thanks!

Sandra_Rossi
Active Contributor
0 Kudos

I hope you understand in these 2 lines of your code where the big error is:

READ TABLE ITAB_1 WITH KEY: PERNR = ITAB_2-PERNR, DAT2 = ITAB_2-DAT1.
IF SY-SUBRC = 0.

Said differently, in ABAP language, do you know what are colon and comma for?

View Entire Topic
xiswanto
Active Participant
0 Kudos

in the case you want to join data from both itab with same index, you could just use

READ TABLE itab_3 INDEX sy-tabix.

right after the loop, unless you have key ( or keys ) which is unique to each row