Application Development and Automation 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: 
Read only

comapring two internal tables

Former Member
0 Likes
853

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
812

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.

6 REPLIES 6
Read only

Former Member
0 Likes
812

Hi thangaraj,

1. simple

2. IF ITAB1[] = ITAB2[].

ENDIF.

3. For assigning to 3rd internal table,

just use

ITAB3[] = ITAB1[]

regards,

amit m.

Read only

Former Member
0 Likes
812

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.

Read only

Former Member
0 Likes
813

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.

Read only

Former Member
0 Likes
812

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

Read only

Lakshmant1
Active Contributor
0 Likes
812

Hi Thangaraj,

Check demo program DEMO_INT_TABLES_COMPARE

Also check FM COMPARE_TABLES

Hope this helps

Thanks

Lakshman

Read only

uwe_schieferstein
Active Contributor
0 Likes
812

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