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

compairing two itab

Former Member
0 Likes
1,573

Dear all,

I have two internal table with same structure but different records.

i want to set a flag if records are different of internal table.

data : itab1 like mara occurs 0 with header line.

data : itab2 like itab occurs 0 with header line.

itab2[] = itab1[].

now suppose i make changes in itab1 .

same structure but different records... what will be answer.

********Point is assured.

1 ACCEPTED SOLUTION
Read only

dev_parbutteea
Active Contributor
0 Likes
1,552

Hi,

i think that you will have to do that in a loop like this :

data:BEGIN OF itab1 OCCURS 10,

bukrs LIKE bkpf-bukrs,

belnr like bkpf-belnr,

gjahr like bkpf-gjahr,

end of itab1.

data:BEGIN OF itab2 OCCURS 10,

bukrs LIKE bkpf-bukrs,

belnr like bkpf-belnr,

gjahr like bkpf-gjahr,

end of itab2.

data:wa_2 like itab2,

v_count2 TYPE i,

v_index TYPE i,

wa_1 like itab1,

v_count1 TYPE i.

move 'BE01' to wa_1-bukrs.

move '01' to wa_1-belnr.

move '2006' to wa_1-gjahr.

append wa_1 to itab1.

move 'BE01' to wa_1-bukrs.

move '02' to wa_1-belnr.

move '2006' to wa_1-gjahr.

append wa_1 to itab1.

move 'BE01' to wa_1-bukrs.

move '03' to wa_1-belnr.

move '2006' to wa_1-gjahr.

append wa_1 to itab1.

move 'BE02' to wa_2-bukrs.

move '01' to wa_2-belnr.

move '2006' to wa_2-gjahr.

append wa_2 to itab2.

move 'BE01' to wa_2-bukrs.

move '02' to wa_2-belnr.

move '2006' to wa_2-gjahr.

append wa_2 to itab2.

move 'BE01' to wa_2-bukrs.

move '03' to wa_2-belnr.

move '2006' to wa_2-gjahr.

append wa_2 to itab2.

sort itab1 by bukrs belnr gjahr.

sort itab2 by bukrs belnr gjahr.

DESCRIBE TABLE itab1 LINES v_count1.

DESCRIBE TABLE itab2 LINES v_count2.

if v_count1 = v_count2.

loop at itab1 into wa_1.

v_index = sy-tabix.

read TABLE itab2 INTO wa_2 INDEX v_index.

if wa_1 <> wa_2.

write:'tables not similar!!'.

exit.

ENDIF.

if v_index = v_count1.

write:'tables similar!!'.

ENDIF.

ENDLOOP.

else.

write:'tables not similar!!'.

ENDIF.

Regards,

Sooness

Dear all,

I have two internal table with same structure but different records.

i want to set a flag if records are different of internal table.

data : itab1 like mara occurs 0 with header line.

data : itab2 like itab occurs 0 with header line.

itab2[] = itab1[].

now suppose i make changes in itab1 .

same structure but different records... what will be answer.

********Point is assured.

14 REPLIES 14
Read only

Former Member
0 Likes
1,552

hi,

If the data is same u can use

itab1[] = itab2[].

If it is different then u can use MOVE.

Read only

Former Member
0 Likes
1,552

Hi,

if not itab1[] = itab2[].

do something

endif.

Reward points if helpful.

Regards.

Srikanta Gope

Read only

Former Member
0 Likes
1,552

IF itab2[] NE itab1[].
  "Records are different
ENDIF.

Or am I looking at it too simply?

Read only

Former Member
0 Likes
1,552

U can compare 2 internal tabs

usining

If itab1[] = itab2[].

message...

endif.

Reward if useful

Read only

Former Member
0 Likes
1,552

hi,

firstyl if u use itab2[] = itab1[] then u are assigning all the values of one internal tabe to another.

both will contain the same records

u can loop one internal table and corresponding read the other internal table using read statement with key.

loop at itab.

read table itab2 with <key> = itab-<key>.

if sy-subrc = 0.

ur code

endif.

endloop.

reward points plz

Read only

Former Member
0 Likes
1,552

Hi,

If u want to compare all fields,

for matching purpose,

then we can do like this.

report abc.

data : a like t001 occurs 0 with header line.

data : b like t001 occurs 0 with header line.

loop at a.

LOOP AT B.

IF A = B.

WRITE 😕 'SAME'.

ENDIF.

endloop.

ENDLOOP.

Regards

Read only

Former Member
0 Likes
1,552

Wow - You've gotta love SDN! 4 answers within 3 minutes of the question being asked!

Read only

Former Member
0 Likes
1,552

Hope this will help u..

Comparing Internal Tables

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

.... <itab1> <operator> <itab2> ...

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

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.


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.
The output is: 
ITAB GT JTAB
ITAB EQ JTAB
JTAB LE ITAB
ITAB NE JTAB
ITAB LT JTAB

This example creates two standard tables, ITAB and JTAB. ITAB is filled with 3 lines and copied to JTAB. Then, another line is appended to ITAB and the first logical expression tests whether ITAB is greater than JTAB. After appending the same line to JTAB, the second logical expression tests whether both tables are equal. Then, another line is appended to ITAB and the third logical expressions tests whether JTAB is less than or equal to ITAB. Next, another line is appended to JTAB. Its contents are unequal to the contents of the last line of ITAB. The next logical expressions test whether ITAB is not equal to JTAB. The first table field whose contents are different in ITAB and JTAB is COL1 in the last line of the table: 30 in ITAB and 50 in JTAB. Therefore, in the last logical expression, ITAB is less than JTAB.

Reward if useful..

Regards

Prax

Read only

dev_parbutteea
Active Contributor
0 Likes
1,552

Hi,

i didn't understand your question well but you can do the following:

1) modify itab1 and make a read statement with all key fields i itab2.

if present then set flag .

2) modify itab1 .

then use modify itab2 where <b>key_field(s) = value(s) from itab1</b>

Regards,

Sooness

Read only

Former Member
0 Likes
1,552

Hi,

this may help u:

*handling duplicates in Internal table:

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

assume that 'itab' is the name of the internal table that holds the sample data that u've posted,

1.copy itab to a temporary table itab_temp

itab_temp[] = itab[].

2. use the following logic.

loop at itab.

clear w_count.

loop at itab_temp where f2 = itab-f2.

w_count = w_count + 1.

delete itab_temp.

endloop.

if w_count > 2.

append itab to itab_final.

endif.

endloop.

3. now the internal table 'itab_final' contains all the required values.

This is my itab1 records:

f1 f2

101 1001

102 1001

103 1003

104 1004

105 1004

106 1004

107 1005

108 1005

Now in any temp table the output should be:

f1 f2

101 1001

102 1001

104 1004

105 1004

106 1004

107 1005

108 1005

Jogdand M B

Read only

Former Member
0 Likes
1,552

Dear All ,

Thanks for your valuable support.

Just i want to compair records of two internal table which having same structure .

like

itab1 itab2 both having 3 records but different data.

itab1-name = 'AAA'. itab2-name = 'BBB'

itab1-age= 34. itab2-age = 35

itab1-salary = $1200. itab2-salary = $1400

how to compair ?

Read only

0 Likes
1,552

Hi,

U can use,

IF itab1[] EQ itab2[].

WRITE 😕 ''Two Tables are Similar'.

ENDIF.

Regards,

Padmam.

Read only

dev_parbutteea
Active Contributor
0 Likes
1,553

Hi,

i think that you will have to do that in a loop like this :

data:BEGIN OF itab1 OCCURS 10,

bukrs LIKE bkpf-bukrs,

belnr like bkpf-belnr,

gjahr like bkpf-gjahr,

end of itab1.

data:BEGIN OF itab2 OCCURS 10,

bukrs LIKE bkpf-bukrs,

belnr like bkpf-belnr,

gjahr like bkpf-gjahr,

end of itab2.

data:wa_2 like itab2,

v_count2 TYPE i,

v_index TYPE i,

wa_1 like itab1,

v_count1 TYPE i.

move 'BE01' to wa_1-bukrs.

move '01' to wa_1-belnr.

move '2006' to wa_1-gjahr.

append wa_1 to itab1.

move 'BE01' to wa_1-bukrs.

move '02' to wa_1-belnr.

move '2006' to wa_1-gjahr.

append wa_1 to itab1.

move 'BE01' to wa_1-bukrs.

move '03' to wa_1-belnr.

move '2006' to wa_1-gjahr.

append wa_1 to itab1.

move 'BE02' to wa_2-bukrs.

move '01' to wa_2-belnr.

move '2006' to wa_2-gjahr.

append wa_2 to itab2.

move 'BE01' to wa_2-bukrs.

move '02' to wa_2-belnr.

move '2006' to wa_2-gjahr.

append wa_2 to itab2.

move 'BE01' to wa_2-bukrs.

move '03' to wa_2-belnr.

move '2006' to wa_2-gjahr.

append wa_2 to itab2.

sort itab1 by bukrs belnr gjahr.

sort itab2 by bukrs belnr gjahr.

DESCRIBE TABLE itab1 LINES v_count1.

DESCRIBE TABLE itab2 LINES v_count2.

if v_count1 = v_count2.

loop at itab1 into wa_1.

v_index = sy-tabix.

read TABLE itab2 INTO wa_2 INDEX v_index.

if wa_1 <> wa_2.

write:'tables not similar!!'.

exit.

ENDIF.

if v_index = v_count1.

write:'tables similar!!'.

ENDIF.

ENDLOOP.

else.

write:'tables not similar!!'.

ENDIF.

Regards,

Sooness

Read only

Former Member
0 Likes
1,552

Thanks to all,

my problem has been solved.

Point has been given.