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 two internal tables

Former Member
0 Kudos

Hi,

I want to compare two internal tables as per their contents.

Is there any standard function module available or any other tools which I can use.

Raj

10 REPLIES 10

Former Member
0 Kudos

Hi,

No, there is no tool like this.

Are your tables got the same structure ?

If so loop on the largest and read on the second one.

( Tips : you can also concatenate all fields of a record in one field and make your comparison on this one )

Jope this helps,

Erwan

0 Kudos

Hello Raj

Assuming that you want to compare two itabs having the same structure there is a standard function module available:

CHANGEDOCUMENT_PREPARE_TABLES

If you are looking for a simplified approach you may have a look at my Wiki posting

[Comparing Two Internal Tables - A Generic Approach|https://wiki.sdn.sap.com/wiki/display/Snippets/ComparingTwoInternalTables-AGeneric+Approach]

Regards

Uwe

Former Member
0 Kudos

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA: ITAB LIKE TABLE OF LINE,

JTAB LIKE TABLE OF LINE.

DO 3 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

APPEND LINE TO ITAB.

ENDDO.

MOVE ITAB TO JTAB.

LINE-COL1 = 10. LINE-COL2 = 20.

APPEND LINE TO ITAB.

IF ITAB GT JTAB.

WRITE / 'ITAB GT JTAB'.

ENDIF.

APPEND LINE TO JTAB.

IF ITAB EQ JTAB.

WRITE / 'ITAB EQ JTAB'.

ENDIF.

LINE-COL1 = 30. LINE-COL2 = 80.

APPEND LINE TO ITAB.

IF JTAB LE ITAB.

WRITE / 'JTAB LE ITAB'.

ENDIF.

LINE-COL1 = 50. LINE-COL2 = 60.

APPEND LINE TO JTAB.

IF ITAB NE JTAB.

WRITE / 'ITAB NE JTAB'.

ENDIF.

IF ITAB LT JTAB.

WRITE / 'ITAB LT JTAB'.

ENDIF.

The output is:

ITAB GT JTAB

ITAB EQ JTAB

JTAB LE ITAB

ITAB NE JTAB

ITAB LT JTAB

This example creates two standard tables, ITAB and JTAB. ITAB is filled with 3 lines and copied to JTAB. Then, another line is appended to ITAB and the first logical expression tests whether ITAB is greater than JTAB. After appending the same line to JTAB, the second logical expression tests whether both tables are equal. Then, another line is appended to ITAB and the third logical expressions tests whether JTAB is less than or equal to ITAB. Next, another line is appended to JTAB. Its contents are unequal to the contents of the last line of ITAB. The next logical expressions test whether ITAB is not equal to JTAB. The first table field whose contents are different in ITAB and JTAB is COL1 in the last line of the table: 30 in ITAB and 50 in JTAB. Therefore, in the last logical expression, ITAB is less than JTAB.

Former Member
0 Kudos

Hi,

loop at it_tab1.

loop at it_tab2.

compare the fields and write ur code.

endloop.

endloop.

Hope it helps you.

Regards

Manjari.

Former Member
0 Kudos

as i posted in the previous post, If the two internal tables have same structure we can do comparison. If they are different, we have to loop one table and read the other.

Former Member
0 Kudos

hi,

If your internal table is having the same structure then, you can directly use the command as

IF itab1 = itab2.
**Your required code.
ENDIF.

This definately works.....

Regards,

Kunjal

Former Member
0 Kudos

hiii

you can compare two internal table like below

here it_prd and it_chs are two different internal tables

clear it_s.
clear it_e.
data: incd type i, incs type i,inci type i, incf type i.
data: it_tmp type ty_t_data with header line.
it_tmp[] = it_prd[].
data: inc type i.
loop at it_prd.

  read table it_chs with key
                             spras  w_langu
                             msehi = it_prd-msehi.

    w_tmp = it_prd-msehi.
  if sy-subrc ne 0.
    write:/ w_tmp.
    append it_prd to it_s.
    incs = incs + 1.
  endif.
  if sy-subrc eq 0.
    append it_prd to it_e.
    endif.
  endloop.

regards

twinkal

bpawanchand
Active Contributor
0 Kudos

Hi

Check this Code snippet

https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/comparingTwoInternalTables-AGeneric+Approach

Regards

Pavan

Former Member
0 Kudos

Loop 1st table

read 2nd table with keys pertaining to 1st table.

0 Kudos

you can compare two internal tables using loop statement by keeping large internal table in outer loop and smmal internal table in inner loop.

loop at itab1 into wa1 . * large internal table

loop at itab2 into wa2 where a = itab1-a.

    • your code

endloop.

endloop.