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

internal table

madan_ullasa
Contributor
0 Likes
1,028

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 :):):)- )

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,007

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

10 REPLIES 10
Read only

Former Member
0 Likes
1,008

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

Read only

0 Likes
1,007

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..

Read only

0 Likes
1,007

U can try this.

Loop at itab1.

read table itab2 key u1 = itab1-t1.

if sy-subrc eq 0.

--

--

endif.

endloop.

Read only

0 Likes
1,007

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.

Read only

0 Likes
1,007

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.

Read only

0 Likes
1,007

Hi,

there is no other go, solution is with you only..

regards

vijay

Read only

0 Likes
1,007

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>

Read only

Former Member
0 Likes
1,007

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

Read only

Former Member
0 Likes
1,007

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...

(new link)

Hope this helps you.

Regards,

Maheswaran.B

Message was edited by: Maheswaran B

Read only

Former Member
0 Likes
1,007

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