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: 

Comparing 2 tables

Former Member
0 Kudos

Hello All,

I am new to ABAP programming. I have simple task to perform.

I have 2 SE11 tables, and in ABAP program, i have to compare 2 tables, if there are some new records or modified(i can get this info by checking a flag, a table field) in table1 then i have to update table2.

Could you please send me a sameple code? I will try to simulate it to my requirement.

Regards,

Ravi

6 REPLIES 6

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

You can loop at the first and read the second with a key to see if it has a correponding record.

Loop at itab1 .

 read table itab2 with key fld1 = itab1-fld1.
if sy-subrc = 0.
* Yes, there is a corresponding record in ITAB2.
endif.

endloop.

REgards,

Rich Heilman

former_member181962
Active Contributor
0 Kudos

select *

from ztab1

into itab1.

select *

from ztab2

into itab2.

loop at itab1.

read table itab2 with key field1 = itab1-field1.

if sy-subrc <> 0.

flag = 'X'.

exit.

endif.

endloop.

if flag = 'X'.

  • modify table ztab2.

endif.

Regards,

Ravi

Former Member
0 Kudos

Might records be deleted from the first table?

Rob

Former Member
0 Kudos

Hai Ravi

Check the following code

LOOP AT ITAB1.

READ TABLE ITAB2 WITH KEY FIELD = ITAB1-FIELD.

IF SY-SUBRC <> 0.

v_flag = 'X'.

ENDIF.

ENDLOOP.

if v_flag = 'X'.

DELETE ITAB1.

endif.

Thanks & regards

Sreeni

Message was edited by: Sreenivasulu Ponnadi

Former Member
0 Kudos

Hello Ravi,

Declare to internal tables.

Say Itab1 & Itab2.

Fetch the data from table 1 & 2 to internal table 1 & 2.

loop at itab1.

read table itab2 with key f1 = itab1=f1.

if sy-subrc NE =0.

flag = 'X'.

endif.

endloop.

Hope this will be useful for u.

Reward the points

Regards,

Vasanth

Former Member
0 Kudos

Hi Ravi,

Consider the code below.


REPORT as.

TYPES : BEGIN OF tp_test,
        item(5),
        quan(2),
        END OF tp_test.
DATA : it_test1 TYPE STANDARD TABLE OF tp_test,
       wa_test1 TYPE tp_test,
       it_test2 TYPE STANDARD TABLE OF tp_test,
       wa_test2 TYPE tp_test,
       it_result TYPE STANDARD TABLE OF tp_test.

<b>*Scenario 1</b>

Internal Table : it_test1
<b>item   quan</b>
mfg03  50
mfg04  100
mfg05  20

Internal Table : it_test2
<b>item   quan</b>
mfg03  50
mfg04  120
mfg05  220

*Loop the second Table

LOOP AT it_test2 INTO wa_test2.

*Read the first table

  READ TABLE it_test1 INTO wa_test1 WITH KEY item = wa_test2-item
                                             quan = wa_test2-quan.


*If no entry present in the first table for entry in second table then append
*then append the second table entry.

  IF sy-subrc NE 0.
    APPEND wa_test2 TO it_result.
  ENDIF.

ENDLOOP.


<b>*Scenario 2</b>

Internal Table : it_test1
<b>item   quan</b>
mfg03  50
mfg04  100
mfg05  20

Internal Table : it_test2
<b>item   quan</b>
mfg03  70
mfg03  80
mfg05  230
mfg04  120
mfg04  160


<b>*Or if there are fields with same values for the key fields (Line items) in both the tables then use this.</b>

LOOP AT it_test1 INTO wa_test1.
  LOOP AT it_test2 INTO wa_test2 WHERE item NE wa_test1-item.

    APPEND wa_test2 TO it_result.
  ENDLOOP.
ENDLOOP.


*Now updating the Table.

MODIFY <DB TABLE name> FROM table it_result.

IF sy-subrc EQ 0.
  WRITE : / 'Table updated successfully'.

ENDIF.


Regards,

Arun Sambargi.

Message was edited by: Arun Sambargi