Skip to Content
author's profile photo Former Member
Former Member

compare two internal tables

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

7 Answers

  • Posted on Aug 22, 2006 at 08:07 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 22, 2006 at 08:07 AM

    Hi hridhaynjili,

    1. Simple

    2. IF ITAB1[] = ITAB2[]

    ENDIF.

    Note : a) both should have same structure

    b) [] is important

    regards,

    amit m.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 22, 2006 at 08:08 AM

    Hi Hridhayanjili,

    Check FM ISB_RM_COMPARE_2_ITABS or COMPARE_TABLES

    Hope this helps

    Thanks

    Lakshman

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 22, 2006 at 08:11 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 22, 2006 at 10:07 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 22, 2006 at 10:25 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 17, 2006 at 07:17 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.