08-26-2008 9:38 AM
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
08-26-2008 9:41 AM
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
08-26-2008 2:21 PM
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
08-26-2008 9:42 AM
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.
08-26-2008 9:43 AM
Hi,
loop at it_tab1.
loop at it_tab2.
compare the fields and write ur code.
endloop.
endloop.
Hope it helps you.
Regards
Manjari.
08-26-2008 9:43 AM
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.
08-26-2008 9:44 AM
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
08-26-2008 9:48 AM
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
08-26-2008 9:49 AM
Hi
Check this Code snippet
https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/comparingTwoInternalTables-AGeneric+Approach
Regards
Pavan
08-26-2008 9:57 AM
08-26-2008 12:38 PM
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.