‎2005 Dec 27 5:21 AM
Hi frnds,
I have 2 itabs- itab1 and itab2. I have to compare every value of a field from itab1 with all the fields of itab2.
its like, i loop at itab1 , then within that loop i loop again at itab2. then i compare the field from itab1 with all the values in a field in itab2.
But this seems like a crude method to me.
is there a better way to handle this.?
regards,
Madan.. ( i always assure points :):):)- )
‎2005 Dec 27 5:31 AM
Hi Madan,
1. If all the fields match,
for all the records,
then both table data are identical.
2. So basically u want to compare
two tables
3. We can use
if ITAB1[] = ITAB2[].
ENDIF.
4 The table body operator []
is important.
It compares the whole body/data.
I hope it helps.
(i think this was what u were asking for)
regards,
amit m.
Message was edited by: Amit Mittal
‎2005 Dec 27 5:31 AM
Hi Madan,
1. If all the fields match,
for all the records,
then both table data are identical.
2. So basically u want to compare
two tables
3. We can use
if ITAB1[] = ITAB2[].
ENDIF.
4 The table body operator []
is important.
It compares the whole body/data.
I hope it helps.
(i think this was what u were asking for)
regards,
amit m.
Message was edited by: Amit Mittal
‎2005 Dec 27 5:40 AM
dear amit,
i know wot u mentioned in ur reply...
but my query is like this ..
itab1 has columns t1 t2.
itab2 has columns u1 u2 .
loop at itab1.
loop at itab2.
if itab1-t1 = itab2-u1.
......
......
endif.
endloop.
endloop.
This works . i kno that. any other better way to do this ?
coz loop within a loop is a non-performer. :)-
one method is to "describe" table itab1 and then,
"do itab1 that many times"...
but this is also a crude method...
regrds,
madan..
‎2005 Dec 27 5:49 AM
U can try this.
Loop at itab1.
read table itab2 key u1 = itab1-t1.
if sy-subrc eq 0.
--
--
endif.
endloop.
‎2005 Dec 27 5:50 AM
Hi again,
1. Thanks for awarding points
inspite of it did not solve ur query.
2. a) are the column count in both tables same ?
b) i assume only the field names are different.
but the field sequence and data types
is same ?
regards,
amit m.
‎2005 Dec 27 5:58 AM
hi amit,
columns count may or may not be same.
data type are same..
i just want to know if there any method in ABAP to compare a complete column of one itab with complete column of another itab.
i think the method i mentioned is the only way out.
‎2005 Dec 27 6:03 AM
Hi,
there is no other go, solution is with you only..
regards
vijay
‎2005 Dec 27 6:09 AM
from memory:
Sort both tables on their respective comparison keys key1t1, keynt1, key1t2, keynt2...
loop at t1
read table t2 binary mode key key1t2 = key1t1
keynt2 = keynt1.
check sys-subrc = 0.
found at least 1 rec in t2
now you can set idx to sys-tabix
loop at t2 from idx.
if keys no longer match exit.
each record here matches the keys in t1...
endloop. 't2
endloop. 't1
Alternative ...
Keep an index for each table and compare to max index.
walk through with 2 cursors and change group
<b>You might want to ensure that your primary table has no duplicates</b>
‎2005 Dec 27 5:42 AM
if both the table fields are same then you can compare body of the internal tables.
but here your question looks different...
in that case what ever you are doing might be the appropriate one(even it crude).
vijay
‎2005 Dec 27 6:03 AM
Hi Madan Kochana,
<b>Comparison of internal tables</b>
DESCRIBE TABLE: TAB1 LINES L1,
TAB2 LINES L2.
IF L1 <> L2.
TAB_DIFFERENT = 'X'.
ELSE.
TAB_DIFFERENT = SPACE.
LOOP AT TAB1.
READ TABLE TAB2 INDEX SY-TABIX.
IF TAB1 <> TAB2.
TAB_DIFFERENT = 'X'. EXIT.
ENDIF.
ENDLOOP.
ENDIF.
IF TAB_DIFFERENT = SPACE.
" ...
ENDIF.
IF TAB1[] = TAB2[].
" ...
ENDIF.
This is document given by sap how to compare two internal tables.
http://help.sap.com/saphelp_erp2005/helpdata/en/fc/eb3653358411d1829f0000e829fbfe/frameset.htm
This topic has already been dicussed. Check these threads...
Hope this helps you.
Regards,
Maheswaran.B
Message was edited by: Maheswaran B
‎2005 Dec 27 6:15 AM
Hi again,
1. U may be right, that , there is no other way.
2. However, with some constraints, like
a) same number of columns in both tables
b) data type is same of each column
c) the field sequence is same
d) The Field names may be DIFFERENT
we can compare the header line
and it will provide what we want
instead of comparing field by field.
( We can also check ITAB1[] = ITAB2[] )
3. Try this code (just copy paste)
ITAB1, ITAB2
both having different field names
but the program observes the above rules.
4.
DATA : BEGIN OF itab1 OCCURS 0,
f1(4) TYPE c,
END OF itab1.
DATA : BEGIN OF itab2 OCCURS 0,
g1(4) TYPE c,
END OF itab2.
DATA : ind TYPE sy-tabix.
*----
SELECT bukrs FROM t001
INTO TABLE itab1.
SELECT bukrs FROM t001
INTO TABLE itab2.
*----
LOOP AT itab2.
IF itab2-g1 = '1000'.
itab2-g1 = 'PPPP'.
MODIFY itab2.
ENDIF.
ENDLOOP.
*----
LOOP AT itab1.
ind = sy-tabix.
READ TABLE itab2 INDEX ind.
IF itab1 = itab2.
ELSE.
WRITE 😕 'not equal' , itab1-f1 , itab2-g1.
ENDIF.
ENDLOOP.
BREAK-POINT.
I hope it helps.
regards,
amit m.
Message was edited by: Amit Mittal