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

Comparing 2 internal Tables

Former Member
0 Likes
7,256

Dear All,

I have two Internal tables with the same structure.

Now i want to compare their contents line by line.

eg.

Loop at IT1.

Read Table IT2 with key MBLNR = IT1-MBLNR.

----


----


EndLoop.

Please help.

Regards

Ankur

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
5,311

Hi Ankur,

first sort both the internal tables.

loop at the first internal table (which has more entries) and read the content of the second internal table(which has less entries) for every occurence of the first internal table and have a count if count increses the tables are different.

*for checking the no of entries in the internal table and looping based on the no of entries

DESCRIBE TABLE itab1 LINES COUNT_1.

DESCRIBE TABLE itab2 LINES COUNT_2.

DATA : count type i default 0, *variable for checking the same content

count_1 type i default 0,

count_2 type i default 0.

SORT ITAB1 field1 field2.

SORT ITAB2 field1 field2.

IF count1 >count2.

LOOP AT itab1 INTO watab1.

READ TABLE itab2 INTO watab2 WITH KEY field1 = 'VALUE'.

IF SY-SUBRC NE 0.

count = count + 1.

ENDIF.

ENDLOOP.

ELSE.

LOOP AT itab2 INTO watab2.

READ TABLE itab1INTO watab1 WITH KEY field1 = 'VALUE'.

IF SY-SUBRC NE 0.

count = count + 1.

ENDIF.

ENDLOOP.

ENDIF.

IF count >0.

write ' both the contents are same'.

ELSE.

write 'Different'.

ENDIF.

Hope it helps u.

regards,

Gopalakrishnan Ulagajothi

14 REPLIES 14
Read only

rainer_hbenthal
Active Contributor
0 Likes
5,311

Cant see where your problem is.

Read only

Former Member
0 Likes
5,311

Hello


sort: IT1, IT2.
if IT1[] = IT2[].
  write: 'Same'.
else.
  write: 'With difference'.
endif.

Read only

Former Member
0 Likes
5,311

I want to compare it line by line.

So i have to perform it in a loop.

I cant use IT1[] = IT2[].

Read only

0 Likes
5,311

Get Number of lines of table1 and table2. if different, tables are different.

The do a DO loop DO lines times.

Read from table 1 with index into workarea1, read from table2 by index into workarea 2. Compare both workareas.

Read only

Former Member
0 Likes
5,311

Hi Ankur,

Your problem is not very much clear.

Use loop instead of Read, because any of two same records, Read statement takes only first record.

Loop at IT1.

loop at IT2 where MBLNR = IT1-MBLNR.

-


-


EndLoop.

Endloop.

Regards,

Vijay

Read only

sreeramkumar_madisetty
Active Contributor
0 Likes
5,311

Hi Ankur

try IT1 [ ] = IT2 [ ] .

Regards,

Sreeram

Read only

Former Member
0 Likes
5,311

Hi,

Before comparing sort bothinternal tables on all the fields & compare line to line.

Read only

Former Member
0 Likes
5,311

Dear All,

I think the problem is not clear to you all. Check the below detail description.

I have 2 Internal table of same structure. Both Internal tables contain 70 fields

Both have a primary key DOCNO.

I have sorted both Internal tables.

Now i want to check if their is any change in contents in IT2 with respect to the contents of IT1-DOCNO.

I have to check the IT2 field by field & Line by line.

Eg.:

IT1 ->

DOCNO EBELN MATNR LIFNR DMBTR

50001 25001 110-001 10021 100

50002 25002 110-001 10021 200

50003 25003 110-001 10021 500

IT2 ->

DOCNO EBELN MATNR LIFNR DMBTR

50001 25001 110-001 10021 300

50002 25002 110-001 10021 200

50003 25003 110-001 10021 600

Now as shown above, I want to compare both the internal tables field by field.

Like: In row 1 & 3 only DMBTR is different. Rest is same.

So, i want to copy such rows in separate IT3.

Please guide me how to code the Loop.

Regards

Ankur

Edited by: Ankur Gupta on Sep 8, 2009 7:39 AM

Read only

0 Likes
5,311

Unclear problems will result in unclear solutions.

Line by line is completly different to field by field.

Just go through the tables and compare the fields. The needed statement is IF. This is just a laborious task.

Loop, read from both tables into different work areas, compare field by field with if. Thats it.

Read only

Former Member
0 Likes
5,311

hi ,

there is two way to do this

1.

loop tab1 .

read table tab2 with key fld1 = itab1-fld1 fld2 = itab1-fld2 fld3 = itab1-fld3 . (compare all fields)

if sy-subrc eq 0.

match .

else .

not match .

endif .

endloop.

2

try to use FM.

CATSXT_COMPARE_STRUCTURES

CTVB_COMPARE_TABLES

Jitendra

Read only

Former Member
0 Likes
5,311

I know this is a laborious task so i cant use if Condition with in a loop.

For that only i am asking if their is any dynamic solution for handling such requirement or if there is any standard function to handle the same.

Read only

0 Likes
5,311

Hello Ankur,

By whichever way you go finally you need to compare using IF statement only. it may be custom code or standard code.

other option may be you can try using SEARCH <itab> FOR <data>.

Thanks,

Augustin.

Read only

Former Member
0 Likes
5,312

Hi Ankur,

first sort both the internal tables.

loop at the first internal table (which has more entries) and read the content of the second internal table(which has less entries) for every occurence of the first internal table and have a count if count increses the tables are different.

*for checking the no of entries in the internal table and looping based on the no of entries

DESCRIBE TABLE itab1 LINES COUNT_1.

DESCRIBE TABLE itab2 LINES COUNT_2.

DATA : count type i default 0, *variable for checking the same content

count_1 type i default 0,

count_2 type i default 0.

SORT ITAB1 field1 field2.

SORT ITAB2 field1 field2.

IF count1 >count2.

LOOP AT itab1 INTO watab1.

READ TABLE itab2 INTO watab2 WITH KEY field1 = 'VALUE'.

IF SY-SUBRC NE 0.

count = count + 1.

ENDIF.

ENDLOOP.

ELSE.

LOOP AT itab2 INTO watab2.

READ TABLE itab1INTO watab1 WITH KEY field1 = 'VALUE'.

IF SY-SUBRC NE 0.

count = count + 1.

ENDIF.

ENDLOOP.

ENDIF.

IF count >0.

write ' both the contents are same'.

ELSE.

write 'Different'.

ENDIF.

Hope it helps u.

regards,

Gopalakrishnan Ulagajothi

Read only

Former Member
0 Likes
5,311

Hey,

Please check withthe following ideas.

1.describe structureof two internal tables.

den create dynamic subroutine and apply loop in one table.

it will take one by one all he fields and compare it with another fields.

3. u are having fundamentals od how to compare.

please

try dis it will work and with optimized code also.