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: 

comparing two internal tables

Former Member
0 Kudos

hi all,

i have to compare two internal tables based on 3 fields and if the lineitems with those fields are matching , i have to delete that line item from the first internal table.

10 REPLIES 10

I355602
Advisor
Advisor
0 Kudos

Hi,

Use something like:-


sort itab1 by field1 field2 field3.
sort itab2 by field1 field2 field3.

loop at itab1.
  read table itab2 with key field1 = itab1-field1
                            field2 = itab1-field2
                            field3 = itab1-field3.
  if sy-subrc = 0.
    delete itab1.
  endif.
  clear itab1.
endloop.

Hope this helps you.

Regards,

Tarun

Former Member
0 Kudos

Hi,

Try like this....



loop at itab into w_itab.                                                                
    delete itab2 where <field name> eq w_itab-<fieldname>.                                   
  endloop.

Hope its helps

Former Member
0 Kudos

Hi

Is the combination of the three fields unique ?

If its is , use read else use loop to check condition

Former Member
0 Kudos

Hi,

Firstly create tmp table of type itab1 or u can also create table with only columns which are common in both tables.

select * into tmp from itab1 inner join itab2 where itab1-field1 = itab2-field1 and itab1-field2 = itab2-field2 and itab1-field3 = itab2-field3.

loop at tmp.

delete from itab1 where itab1-field1 = tmp-field1 and itab1-field2 = tmp-field2

and itab1-field3 = tmp-field3.

endloop.

Thanks,

Smita

former_member632729
Contributor
0 Kudos

Hi Dude,


sort table1 by field1 field2 field3.
sort table2 by field1 field2 field3.
 
loop at table1 into wa_table1.
  read table table2 into wa_table2 with key field1 = wa_table1-field1
                            field2 = wa_table1-field2
                            field3 = wa_table1-field3.
  if sy-subrc = 0.
    delete table table1 from wa_table1.
  endif.
  clear table1.
endloop.

Former Member
0 Kudos

Hi use something below to sort the two internal tables after tables having all data .

sort table1 by field1 field2 field3."sorting two internal tables

sort table2 by field1 field2 field3.

loop at table1 into wa_table1."Comparing record by record whether they are equal.

read table table2 into wa_table2 with key field1 = wa_table1-field1

field2 = wa_table1-field2

field3 = wa_table1-field3.

if sy-subrc = 0." if yes delete

delete table table1 from wa_table1.

endif.

clear table1.

endloop.

Regards

Former Member
0 Kudos

hi,



sort int_table1 by field1 field2 field3.
sort int_table2 by field1 field2 field3.

loop at int_table1 into wa_table1.

 delete int_table2 where field1 = wa_table1-field1  OR field2 = wa_table1-field2  OR field3 = wa_table1-field3.

endloop.


Regards

Ritesh J

Former Member
0 Kudos

loop at itab.

read table itab1 with key fieldname1 = itab-fieldname1

fieldname2 = itab-fieldname2.

if sy-subrc = 0.

delete itab.

endif.

endloop.

Regards,

Joan

Former Member
0 Kudos

Hi Santosh,

You can do like this:



Suppose you have two internal tables itab1 and itab2 having common field filed1 between them then,

Loop at itab1 into wa1 .

Read table itab2 into wa2 with key field1 = wa1-field1.

if sy-subrc = 0.       "this will give 0 if there is any common data between the two internal tables data

delete itab1 from wa1.

endif.

endloop.

Hope it helps

Regrds

Mansi

Edited by: MANSI ASNANI on Mar 4, 2009 8:44 AM

Former Member
0 Kudos

Hi Santosh.

The following code is working and will meet ur purpose.

TYPES: BEGIN OF T_ITAB1,

FLD1(5) TYPE C,

FLD2(5) TYPE C,

FLD3(5) TYPE C,

END OF T_ITAB1.

TYPES: BEGIN OF T_ITAB2,

FLD1(5) TYPE C,

FLD2(5) TYPE C,

FLD3(5) TYPE C,

END OF T_ITAB2.

DATA: ITAB1 TYPE STANDARD TABLE OF T_ITAB1,

ITAB2 TYPE STANDARD TABLE OF T_ITAB2,

WA_ITAB1 LIKE LINE OF ITAB1,

WA_ITAB2 LIKE LINE OF ITAB2.

WA_ITAB1-FLD1 = 'AAAA'.

WA_ITAB1-FLD2 = '1111'.

WA_ITAB1-FLD3 = '2222'.

APPEND WA_ITAB1 TO ITAB1.

CLEAR WA_ITAB1.

WA_ITAB1-FLD1 = 'BBBB'.

WA_ITAB1-FLD2 = '6666'.

WA_ITAB1-FLD3 = '7777'.

APPEND WA_ITAB1 TO ITAB1.

CLEAR WA_ITAB1.

  • *********************************

WA_ITAB2-FLD1 = 'AAAA'.

WA_ITAB2-FLD2 = '1111'.

WA_ITAB2-FLD3 = '2222'.

APPEND WA_ITAB2 TO ITAB2.

CLEAR WA_ITAB2.

WA_ITAB2-FLD1 = 'CCCC'.

WA_ITAB2-FLD2 = '8888'.

WA_ITAB2-FLD3 = '9999'.

APPEND WA_ITAB2 TO ITAB2.

CLEAR WA_ITAB2.

*************************************

LOOP AT ITAB1 INTO WA_ITAB1.

LOOP AT ITAB2 INTO WA_ITAB2.

IF WA_ITAB2-FLD1 = WA_ITAB1-FLD1 AND WA_ITAB2-FLD2 = WA_ITAB1-FLD2.

DELETE ITAB1.

ENDIF.

ENDLOOP.

ENDLOOP.

Thanks and Regards

Suraj S Nair