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: 

Compare two internal tables to find difference in data

Former Member
0 Kudos

Hi All,

I am trying to differentiate between data of two internal tables(they have similar structure). One internal table will have persisted data and other internal table contains the daily data. So I need to check data which is updated or not there in older internal table. For that I need to compare every field of two tables.

Thanks,

Shailesh S. Malkar.

7 REPLIES 7

Former Member
0 Kudos

Dear Shailesh,

Does both the tables have same number of records?

Thanks,

Manikandan JN.

0 Kudos

Dear Manikandan,

They may have same number of records. But data in may vary as second table contains the daily data. So there can be change in data. And I need to find that only. The data which is changed from older one.

Thanks,

Shailesh S. Malkar.

0 Kudos

Dear Shailesh,

Try this code. Do not look at the meaning of this code. Just look at the logic.

DATA: it_mara1 TYPE STANDARD TABLE OF mara,
       it_mara2 TYPE STANDARD TABLE OF mara,
       it_mara_final TYPE STANDARD TABLE OF mara,
       wa_mara1  TYPE mara,
       wa_mara2  TYPE mara,
       lv_index TYPE sy-index.

CLEAR wa_mara1.
wa_mara1-matnr = '1234'.
APPEND wa_mara1 TO it_mara1.

CLEAR wa_mara1.
wa_mara1-matnr = '5678'.
APPEND wa_mara1 TO it_mara1.

CLEAR wa_mara1.
wa_mara1-matnr = '1234'.
APPEND wa_mara1 TO it_mara2.

CLEAR wa_mara2.
wa_mara2-matnr = '5678'.
APPEND wa_mara2 TO it_mara2.

CLEAR wa_mara2.
wa_mara2-matnr = '5679'.
APPEND wa_mara2 TO it_mara2.

lv_index = 1.

LOOP AT it_mara2 INTO wa_mara2 FROM lv_index TO lv_index.

   READ TABLE it_mara1 INTO wa_mara1 INDEX lv_index.
   IF sy-subrc = 0.
     IF wa_mara1 NE wa_mara2.
       APPEND wa_mara2 TO it_mara_final.
     ENDIF.
   ELSE.
     APPEND wa_mara2 TO it_mara_final.
   ENDIF.

   lv_index = lv_index + 1.

ENDLOOP.

Here assume that IT_MARA2 is your daily table and IT_MARA1 is your persistant table.

IT_MARA_FINAL will collect the changed/updated data.

Thanks,

Manikandan JN.

0 Kudos

Shailesh, BTW I have assumed that the two tables have records in the same order(if not, sort the tables before proceeding) and any new records will be appended at the end of the table.

If this is not your case, please post your queries

Thanks,

Manikandan JN.

Former Member
0 Kudos

You can do a LOOP at Persisted data and Read the daily data inside that Loop.

Thanks,

Shambu

Former Member
0 Kudos

Hi Shailesh,

maybe one of these function modules fits your requirements:

CULI_L_SET_DIFFERENCE

CTVB_COMPARE_TABLES

CTVB_COMPARE_TABLES_2

Andreas

andrey_ryzhkov
Participant
0 Kudos

Hi,

you can use FM CHANGEDOCUMENT_PREPARE_TABLES - it will separate new, modified and deleted entries. The only thing that you need is to create two dynamic structures for your tables that you compare as described here in method compare: Comparing Two Internal Tables - A Generic Approach - Code Gallery - SCN Wiki