‎2006 Sep 01 2:17 PM
hi genius
i am having two internal tables itab1 and itab2.i would like to compare these two tables(say each having five fields f1,f2,f3,f4,f5 same structure)and if two tables are equal,i should move that into itab3,during comparision if one field of itab1(f2) dosent match with the field of itab2(f2) it should raise an error.how to do that.
thanks in advance
‎2006 Sep 01 2:32 PM
Hi,
check this sample prog
data:begin of itab1 occurs 0,
f1(10) type c,
end of itab1.
data:begin of itab2 occurs 0,
f1(10) type c,
end of itab2.
data:begin of itab3 occurs 0,
f1(10) type c,
end of itab3.
itab1-f1 = 'abc'.
append itab1.
itab2-f1 = 'abc'.
append itab2.
if itab1[] = itab2[].
itab3[] = itab1[].
else.
write:/ 'tables are not same'. (or)
message e000(zz) with 'tables are not same'.
endif.
loop at itab3.
write:/ itab3-f1.
endloop.
Regards,
Sowjanya.
‎2006 Sep 01 2:26 PM
Hi thangaraj,
1. simple
2. IF ITAB1[] = ITAB2[].
ENDIF.
3. For assigning to 3rd internal table,
just use
ITAB3[] = ITAB1[]
regards,
amit m.
‎2006 Sep 01 2:31 PM
Hi Thangaraj,
LOOP AT t_1.
READ TABLE t_2 WITH KEY f1 = t_1-f1
f2 = t_1-f2.
IF sy-subrc = 0.
MOVE-CORRESPONDING t_1 TO t_3.
APPEND t_3.
CLEAR t_3.
ELSE.
raise error
ENDIF.
ENDLOOP.
‎2006 Sep 01 2:32 PM
Hi,
check this sample prog
data:begin of itab1 occurs 0,
f1(10) type c,
end of itab1.
data:begin of itab2 occurs 0,
f1(10) type c,
end of itab2.
data:begin of itab3 occurs 0,
f1(10) type c,
end of itab3.
itab1-f1 = 'abc'.
append itab1.
itab2-f1 = 'abc'.
append itab2.
if itab1[] = itab2[].
itab3[] = itab1[].
else.
write:/ 'tables are not same'. (or)
message e000(zz) with 'tables are not same'.
endif.
loop at itab3.
write:/ itab3-f1.
endloop.
Regards,
Sowjanya.
‎2006 Sep 01 2:44 PM
HI,
if (itab1[] = itab2[]).
itab3[] = itab1[].
else.
"raise the error here.
endif.
(itab1[] = itab2[]). this statemetn compares all the contents of the two tables and itab3[] = itab1[]. statement assigns all the contents of itab1 (all the rows) to the itab2.
Regards,
Richa
‎2006 Sep 01 3:07 PM
Hi Thangaraj,
Check demo program DEMO_INT_TABLES_COMPARE
Also check FM COMPARE_TABLES
Hope this helps
Thanks
Lakshman
‎2006 Sep 03 11:23 PM
Hello Thangaraj
The most detailed comparison is to use the same logic as change documents are prepared. Assuming both of your itabs are of structure struc_a. Then define the following type:
TYPES: BEGIN OF ty_s_itab_di.
INCLUDE TYPE struc_a.
TYPES: CHIND TYPE bu_chind.
TYPES: END OF ty_s_itab_di.
TYPES: ty_t_itab_di TYPE STANDARD TABLE OF ty_s_itab_di
WITH DEFAULT KEY.
DATA:
gt_itab_old TYPE ty_t_itab_di,
gt_itab_new TYPE ty_t_itab_di.Fill itabs gt_Itab_old with the corresponding data of itab1 and gt_itab_new with the corresponding data of itab2.
Very important: sort you itabs either by all key fields or by all fields.
Call function CHANGEDOCUMENT_PREPARE_TABLES with the following parameters
- CHECK_INDICATOR = ' '
- TABLE_NEW = gt_Itab_new
- TABLE_OLD = gt_itab_old
The function module will remove identical lines from both itabs. New entries in gt_itab_New will have CHIND = 'I' and deleted entries in gt_itab_old will have CHIND = 'D'.
Read the documentation of the function module and play around with it. You will see that this a quite easy yet powerful approach for comparing itabs.
Regards
Uwe