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

Optimise the code below

Former Member
0 Likes
1,725

Hi Gurus,

Please see the code below . I want avoid the loop at itab1 just for processing a single field.

select * from <dbtable> into corresponding fields of table itab1

where city eq 'value1'

and pstatus eq 1.

<b> loop at itab1.

shift itab1-field1 left deleting leading '0'.

modify itab1 index sy-tabix.

endloop.</b>

loop at itab2.

read table itab2 with key field1 = itab2-field1.

.........

..........

..........

endloop.

Please suggest an optimised code to do the above in a single shot.

Thanks

Dany

1 ACCEPTED SOLUTION
Read only

Clemenss
Active Contributor
0 Likes
1,707

Hi Dan,

1. use loop ... assigning <field-symbol>. This is up to 5 times faster

2. define the table as SORTED or better HASHED and read with TABLE KEY.

3. Avoid SELECT *. Select the fields you really need.

4. Don't worry about INTO CORRESPONDING FIELDS of. Although people say so it does not cost time that can be measured.

Regards,

Clemens

20 REPLIES 20
Read only

former_member156446
Active Contributor
0 Likes
1,707

Hi Dan

remove into corresponding fields of and specify in the same order...

when u read the table first sort it with the key u read it and say Binary search

with binary search ur code will be twice faster and before...

Award points if usefull

Read only

naimesh_patel
Active Contributor
0 Likes
1,707

I understand that you need to modify the itab1 because you have to read the table in other loop.

You can do something like this to avoid the loop on ITAB1.


*loop at itab1.
*shift itab1-field1 left deleting leading '0'.
*modify itab1 index sy-tabix.
*endloop.


loop at itab2.
unpack itab2-field1 to l_field1.   " << set the L_FIELD1 with ZEROs padded 
read table itab1 with key field1 = l_field1.
.........
..........
..........
endloop.

Regards,

Naimesh Patel

Read only

0 Likes
1,707

Hi Naimesh,

i need to use itab2 as it is inevitable.. Any other suggestion.

thanks

Read only

0 Likes
1,707

Hi Naimesh,

i need toloop at itab2. it is inevitable. it has more entries than itab1

loop at itab2.

read table itab1 with key field1 = itab2-field1.

.........

..........

..........

endloop.

Any other suggestion.

thanks

Read only

0 Likes
1,707

Hi Dan,

I believe there won't be able to get rid of that first loop because it's a preliminary process that you need so you can match it through the read table in the second loop. You won't be able to modify that single field, unless you got another way(field) to link the itab1 and itab2 that isn't field1.

Hope it helps.

Regards,

Gilberto Li

Read only

0 Likes
1,707

Maybe something like:

SELECT * FROM <dbtable> INTO CORRESPONDING FIELDS OF TABLE itab1
WHERE city EQ 'value1'
AND pstatus EQ 1.

*loop at itab1.
*shift itab1-field1 left deleting leading '0'.
*modify itab1 index sy-tabix.
*endloop.


LOOP AT itab2.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = itab2-field1
    IMPORTING
      output = itab2-field1.

  READ TABLE itab1 WITH KEY field1 = itab2-field1.
  .........
  ..........
  ..........
ENDLOOP.

Rob

Read only

0 Likes
1,707

Gentlemen - Thanks to all.

My main requirement is to filter out MISMATCHED records between itab 1 & itab 2. itab2 has MORE records than itab 1..So obviuosly i need to use l:

loop at itab2.

read table itab2 with key field1 = itab2-field1.

.........

..........

..........

endloop.

Any more suggestions?

Read only

0 Likes
1,707

In your first and last posts you are only looking at itab2.

Rob

Read only

0 Likes
1,707

Oh! sorry ROB that's a typo error

loop at itab2.

read table itab1 with key field1 = itab2-field1.

endloop.

Read only

0 Likes
1,707

So itab2 has more records than itab1 and some records in itab2 do not exist in itab1. Is that correct?

Rob

Read only

0 Likes
1,707

I'm thinking of something like

loop at itab2.

read table itab1 TRANSPORTING NO FIELDS

with key field1 = itab2-field1.

if sy-subrc eq 0.

delete itab2

endif.

endloop.

Read only

0 Likes
1,707

Rob u r right..

Read only

0 Likes
1,707

Can there be records in itab1 that are not in itab2?

Rob

Read only

0 Likes
1,707

Yes

Read only

0 Likes
1,707

And you want the records that are in itab1, but not itab2 and the records that are in itab2 but not itab1?

Rob

Read only

0 Likes
1,707

Hi Rob,

I want all the uncommon entries between both of them.

thanks

Read only

0 Likes
1,707

Hi Dan,

Do like this

Loop at itab1 into wa1.
 Read Table itab2 into wa2 with key <key field> = wa1-key field.
 If sy-subrc <> 0.
  append wa2 to itab3. " ITAB3 will have uncommon entries.
  clear wa2.
 endif.
endloop.

Regards,

Satish

Read only

0 Likes
1,707

OK - you can use the parallel cursor technique to go through each table once only and keep only the records you want.

Rob

Read only

Clemenss
Active Contributor
0 Likes
1,708

Hi Dan,

1. use loop ... assigning <field-symbol>. This is up to 5 times faster

2. define the table as SORTED or better HASHED and read with TABLE KEY.

3. Avoid SELECT *. Select the fields you really need.

4. Don't worry about INTO CORRESPONDING FIELDS of. Although people say so it does not cost time that can be measured.

Regards,

Clemens

Read only

Former Member
0 Likes
1,707

Hi Dan,

try the other way round and skip the second loop like this:

ranges: rdel1 for itab2-field1.

select * from <dbtable> into corresponding fields of table itab1
where city eq 'value1'
and pstatus eq 1.

rdel1-sign = 'E'. "depends on which entries you want to delete
*                  'E' for unmatched - 'I' for matched
rdel1-option = 'EQ'.
loop at itab1.
  write itab1-field1 to rdel1-low using edit-mask '__________'. 
append rdel1.
endloop.

delete itab2 where field1 in rdel1.

Regards

JW