09-29-2008 8:58 PM
HI all,
I have two internal tables.
tab1 contains 20 records with a/c number as the key field
tab2 contains 15 records of tab1 with the a/c number as the key field.
Now I want to get only the 5 records from the tab1 which is not in tab2.
How do I do this.
Can any one give some suggestions.
Thanks
Shree.
09-29-2008 9:49 PM
Assuming that the number of records that you gave is just an example and that you actually have to process a larger number of records, there are a few ways to process the records:
- loop + loop
- loop + sequential read
- loop + binary search
- loop + loop using a parallel cursor
These will of course vary in performance. Your best tehnique for comparing two tables with a large number of records would be a parallel cursor technique.
A parallel cursor technique will actaully get you a performance of n+m (where n is the number of records in the first table and m is the number of records in the second)
The following is an example of this code
v_pos1 = 0.
clear wa_trdir1.
loop at i_trdir1 into wa_trdir1.
clear v_found.
clear wa_trdir2.
loop at i_trdir2 into wa_trdir2
from v_pos1.
v_pos1 = sy-tabix.
if wa_trdir2-name < wa_trdir1-name.
write:/ wa_trdir2-name, 'in table 2 but not in table 1'.
elseif wa_trdir2-name > wa_trdir1-name.
exit.
else.
v_found = 'X'.
endif.
clear wa_trdir2.
endloop.
if v_found is initial.
write:/ wa_trdir1-name, 'in table 1 but not in table 2'.
else.
write:/ wa_trdir1-name, 'in table 1 and in table 2'.
endif.
clear wa_trdir1.
endloop.
~Ian
09-29-2008 9:04 PM
You can loop on ITAB1 and read the ITAB2 with the same account number.
If you found the entry don't do anything
If you don't found the entry than move it to some other table and update some flag column in the ITAB1.
Regards,
Naimesh Patel
09-29-2008 9:44 PM
Hi,
Same as what Naimesh said.......
Loop at itab1.
read table itab2 with key a/c number = itab1-a/c number.
if sy-subrc ne 0.
itab1-flag = 'X'.
modify itab1.
endif.
endloop.
Now you have 5 records flagged in itab1 which are the different from itab2.
Regards,
DNP
09-29-2008 9:49 PM
Assuming that the number of records that you gave is just an example and that you actually have to process a larger number of records, there are a few ways to process the records:
- loop + loop
- loop + sequential read
- loop + binary search
- loop + loop using a parallel cursor
These will of course vary in performance. Your best tehnique for comparing two tables with a large number of records would be a parallel cursor technique.
A parallel cursor technique will actaully get you a performance of n+m (where n is the number of records in the first table and m is the number of records in the second)
The following is an example of this code
v_pos1 = 0.
clear wa_trdir1.
loop at i_trdir1 into wa_trdir1.
clear v_found.
clear wa_trdir2.
loop at i_trdir2 into wa_trdir2
from v_pos1.
v_pos1 = sy-tabix.
if wa_trdir2-name < wa_trdir1-name.
write:/ wa_trdir2-name, 'in table 2 but not in table 1'.
elseif wa_trdir2-name > wa_trdir1-name.
exit.
else.
v_found = 'X'.
endif.
clear wa_trdir2.
endloop.
if v_found is initial.
write:/ wa_trdir1-name, 'in table 1 but not in table 2'.
else.
write:/ wa_trdir1-name, 'in table 1 and in table 2'.
endif.
clear wa_trdir1.
endloop.
~Ian