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: 

hI there, need help in thinking of a logic.

Former Member
0 Kudos

Good Day!

I'm having trouble thinking of the logic in this simple (but tediously hard for me) problem.

I've got two internal tables, say table A and table B. I need to output records from table A that do not match records from table B. and vice versa.

The thing is, before I can output them, there are 3 primary keys for each table. let's say, ID, Document, and date. I need to match 3 of them to the other table, and if they don't match I would have to print them out.

What I did was.

Loop at TABLEA.

Loop at TABLEB.

IF TABLEA-ID NE TABLEB-ID AND ...

WRITE TABLEA-ID ...

ENDIF.

ENDLOOP.

ENDLOOP.

The problem is, let's say table a has

ID: 0

Doc:1

Date: 1

ID:1:

DOC:1

DATE:1

table b has

ID:1

DOC:1

DATE:1

and so on..

the loop will first compare table a with b and if it doesn't match well.. it prints it out immediately. Maybe the 1st record of table A is in the 20th record of table B. So I get multiple unmatches. So now I'm turning to the experts here, hope you guys can help.

I'm not sure if there was a faster way to do this. Any suggestions would be appreciated.

Edited by: Katrina Dy on Jul 2, 2008 8:56 AM

1 ACCEPTED SOLUTION

JozsefSzikszai
Active Contributor
0 Kudos

hi Katrina,

you have to do something like this:

LOOP AT itab1.
READ TABLE itab2 WITH KEY ... TRANSPORTING NO FIELDS.
IF sy-subrc EQ 0.
* there is a matching record in itab2
ELSE.
* there is no matching record in itab2
ENDIF.
ENDLOOP.

* Now you do vica-versa:

LOOP AT itab2.
READ TABLE itab1 WITH KEY ... TRANSPORTING NO FIELDS.
IF sy-subrc EQ 0.
* there is a matching record in itab1
ELSE.
* there is no matching record in itab1
ENDIF.
ENDLOOP.

hope this helps

ec

11 REPLIES 11

JozsefSzikszai
Active Contributor
0 Kudos

hi Katrina,

you have to do something like this:

LOOP AT itab1.
READ TABLE itab2 WITH KEY ... TRANSPORTING NO FIELDS.
IF sy-subrc EQ 0.
* there is a matching record in itab2
ELSE.
* there is no matching record in itab2
ENDIF.
ENDLOOP.

* Now you do vica-versa:

LOOP AT itab2.
READ TABLE itab1 WITH KEY ... TRANSPORTING NO FIELDS.
IF sy-subrc EQ 0.
* there is a matching record in itab1
ELSE.
* there is no matching record in itab1
ENDIF.
ENDLOOP.

hope this helps

ec

former_member188827
Active Contributor
0 Kudos

write;'Records of tab a dat dont match with tab b'.

loop at itaba.

read table itabb with key id = itaba-id...

if sy-subrc ne 0.

write/ itaba-id...

endif.

endloop.

repeat da same procedure for tab b.

Former Member
0 Kudos

Hi Katrina,

Use READ statement instead of inner LOOP...ENDLOOP.

Loop at TABLEA.

READ TABLE TABLEB WITH KEY ID = TABLEA-ID DOC = TABLEA-DOC DATE = TABLEA-DATE.

IF SY-SUBRC NE 0.

WRITE TABLEA-ID ...

ENDIF.

ENDLOOP.

Thanks,

Vinay

Former Member
0 Kudos

Loop at TABLEA.

Loop at TABLEB.

IF TABLEA-ID NE TABLEB-ID AND ...

flag= 'X'.

ENDIF.

ENDLOOP.

if flag ='X'

WRITE TABLEA-ID ...

endif.

ENDLOOP

Reward IF,,,,,,,,,,,,,,

Regards

Anbu

former_member195383
Active Contributor
0 Kudos

Loop at TABLEA.

Read table TABLEB with key ID = TABLEA-ID

and ....

if sy-subrc eq 0.

WRITE TABLEA-ID ...

ENDIF.

ENDLOOP.

try the above....

Reward points if it helps....

Former Member
0 Kudos

Hi Katrina,

If your both tables 'A' and 'B' have same fields and same primary keys, then move the records of one table to other .

Suppose moving contents of A to B.

Loop at table A .

Move tableA to tableB.

append table B.

endloop.

Sort table B.

Delete adjustent duplicates of table B comparing the primary keys.

And print the table B.

Best regards,

Brijesh

vinod_vemuru2
Active Contributor
0 Kudos

Hi Katrina,

Use parallel cursor methid which is very fast in performance

Check below logic.

DATA l_tabix TYPE sy-tabix VALUE 1.

SORT: tablea BY id dic date,

tableb BY id doc date.

Loop at TABLEA.

Loop at TABLEB FROM l_tabix.

IF TABLEA-ID NE TABLEB-ID AND ...

WRITE TABLEA-ID ...

ELSE.

l_tabix = sy-tabix.

ENDIF.

ENDLOOP.

ENDLOOP.

Also try to avoid using headerlines.

Thanks,

Vinod.

Former Member
0 Kudos

Hello



sort tablea by ID Document date.
sort tableb by ID Document date.
loop at tablea.
  read tableb with key ID = tablea-ID
                                Document = tablea-Document
                                date = tablea-date
                                binary search.
  if sy-subrc NE 0.
    write: tablea-ID, ...
  endif.
endloop.
loop at tableb.
  read tablea with key ID = tableb-ID
                                Document = tableb-Document
                                date = tableb-date
                                binary search.
  if sy-subrc NE 0.
    write: tableb-ID, ...
  endif.
endloop.

Former Member
0 Kudos

I have seen you code,

Instead of directly writing the dissimilar records, take all the records into an interna table ( it_table3 ).

After this to avoid repetetion of records,

Delete adjascent duplicates from it_table3 comparing f1 f2 f3....

Then do the writing of the records.

loop at it_table3.

write:/ it_table3-f1, it_table3-f2, it_table3-f3.

endloop.

Hope this would help.

Reward if found use full.

Cheers.

ram.

Former Member
0 Kudos

Instead of loop use select statement

SELECT tablea-id

FOR ALL ENTRIES IN tableb

FROM tablea

INTO res_table

WHERE id NE tableb-id.

This will select the id of table A which is not in table B.

The above code performs: tableA entries will matches against the entries in the tableB and if it found not equals, then the data which is not equal is fetched in the res_table.

Check for sy-subrc = 0. then print the res_table which will have the resultant id.

Former Member
0 Kudos

Thanks everyone, I tried the Read table instead of Loop and it seems to be working fine now.

I'll reward points. Thanks again