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

Former Member
0 Kudos
3,878

hi everybody

i have two internal tables ITAB1 AND ITAB2

I WANT TO COMPARE THE CONTENTS OF THESE INTERNAL TABLES HOW TO DO THIS

REGARDS

HRIDHANJILI

8 REPLIES 8

dani_mn
Active Contributor
0 Kudos
137

HI,

<b>LOOP at itab1.

READ TABLE itab2 with key field1 = itab1-field1.

if sy-subrc = 0.

write:/ itab1-field1, 'found' color col_positive.

else.

write:/ itab1-fiels1, 'not found' color col_negative.

endif.

ENDLOOP.</b>

Regards,

HRA

Former Member
0 Kudos
137

Hi hridhaynjili,

1. Simple

2. IF ITAB1[] = ITAB2[]

ENDIF.

Note : a) both should have same structure

b) [] is important

regards,

amit m.

0 Kudos
137

Hi,

addition to Amit:

by itabs (type standard/sorted/hashed tables ) without headerline you can compare like this:

if itab1 = itab2.

A.

Lakshmant1
Active Contributor
0 Kudos
137

Hi Hridhayanjili,

Check FM ISB_RM_COMPARE_2_ITABS or COMPARE_TABLES

Hope this helps

Thanks

Lakshman

Former Member
0 Kudos
137

Hi,

Like other data objects, you can use internal tables as operands in logical expressions.

e.g. IF <b>itab1 <operator> itab2.</b>

For <operator>, all operators listed in the table in Comparisons Between Data Types can be used (<b>EQ, =, NE, <>, ><, GE, >=, LE, <=, GT, >, LT, <).</b>

If you are using internal tables with header lines, remember that the header line and the body of the table have the same name. If you want to address the body of the table in a comparison, you must place two brackets ([ ]) after the table name.

The first criterion for comparing internal tables is the number of lines they contain. The more lines an internal table contains, the larger it is. If two internal tables contain the same number of lines, they are compared line by line, component by component. If components of the table lines are themselves internal tables, they are compared recursively. If you are testing internal tables for anything other than equality, the comparison stops when it reaches the first pair of components that are unequal, and returns the corresponding result.

This will definitely help you.

Regards,

Pragya

uwe_schieferstein
Active Contributor
0 Kudos
137

Hello Hridhayanjili

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

Former Member
0 Kudos
137

Hi HRIDHANJILI,

Check with this test code and if helpful Reward points.

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.

Former Member
0 Kudos
137

hi hridhayanjili yaarlagadda try this

LOOP AT ITAB1.

read table itab2 index i.

if itab2-line = itab1-line.

write:/'correct'.

else.

write:/'incorrect'.

endif.

endloop.