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

Tables completion

Former Member
0 Likes
635

Hi experts,

I have 2 tabes ; Table 1 and Table 2.

Table 1 has more records then table 2 and both of them have a common field, let's say fieldY.

I would like to add in table 1 all the entries present in table 2 and not in table 1 and ignore the common records between both tables.

I tought about the following:

Copy the content of table 2 in copytabe2.

Loop at copytabe2 into wa.

     Read table 1 with key fieldY = wa-fieldY.

     if sy-subrc eq 0.

     delete copytabe2 .

     endif.

endloop.

Append lines of copytable2 into table1.

Thanks for your support.

Amine

PS:All the tables are internal tables in a program.

1 ACCEPTED SOLUTION
Read only

venkat_aileni
Contributor
0 Likes
588

Hi-

This is what I would suggest.

APPEND LINES OF ITAB2 TO ITAB1.

SORT ITAB1 BY <Common Field>.

DELETE ADJACANT DUPLICATES FROM ITAB1 COMPARING <Common Field>.

-Venkat

4 REPLIES 4
Read only

venkat_aileni
Contributor
0 Likes
589

Hi-

This is what I would suggest.

APPEND LINES OF ITAB2 TO ITAB1.

SORT ITAB1 BY <Common Field>.

DELETE ADJACANT DUPLICATES FROM ITAB1 COMPARING <Common Field>.

-Venkat

Read only

0 Likes
588

Hi Venkat,

Thanks for your answer. I actually tought about this possiblity. In case where table 1 and Table 2 are different in term of structure. I guess that i can't append lines of itab1 into itab2 directly.

I have to create a structure (str+tabstr) simlar to itab1, then:

loop at itab2 into wa.

str = wa.

append str into tabstr.

endloop.

Can you confirm me?

Thanks.

Amine

Read only

0 Likes
588

Agreed!

Approach 1:

ITAB1, WA1, ITAB2 and WA2.

LOOP AT itab2 INTO wa2.

   MOVE-CORRESPONDING wa2 TO wa1.

   APPEND wa1 TO itab1.

ENDLOOP.

SORT itab1 BY fieldy.

DELETE ADJACENT DUPLICATES FROM itab COMPARING fieldy.

Approach 2:

DATA: lv_tabix TYPE sy-tabix.

LOOP AT itab2 INTO wa2.

   CLEAR: wa1,

                lv_tabix.

   lv_tabix = sy-tabix.

   READ TABLE itab1 INTO wa1 WITH KEY fieldy = wa2-fieldy.

   IF sy-subrc = 0.

     DELETE itab2 INDEX lv_tabix.

   ENDIF.

ENDLOOP.

-Venkat

Read only

0 Likes
588

Thanks a lot Venkat.

Amine