Best way to Loop At ‘Standard’ Internal Table in ABAP

When it comes to looping thru a standard Internal Table, almost every ABAPer normally loops like this (I_COEP is already sorted)
(1) Loop at I_COEP where kokrs = i_cobk-kokrs
and belnr = i_cobk-belnr.
……
endloop.

And this the SLOWEST method to be adopted.

The next best method is accessing the right set of data, using Binary Search, and then read thru it.

(2) read table i_coep with key
kokrs = i_cobk-kokrs
belnr = i_cobk-belnr
binary search.
if sy-subrc = 0.
Loop at i_coep from sy-tabix
where kokrs = i_cobk-kokrs
and belnr = i_cobk-belnr.
…….
Endloop.
endif.

But ‘the best of the lot’ goes to the code, which is the extension of second method defined above. This code exits when no matching criteria is found and thus avoid looping thru the whole internal table.

(3) read table i_coep with key
kokrs = i_cobk-kokrs
belnr = i_cobk-belnr
binary search.
if sy-subrc = 0.
Loop at i_coep from sy-tabix.
if ( i_coep-kokrs NE i_cobk-kokrs or
i_coep-belnr NE i_cobk-belnr )
exit.
endif.
Endloop.
endif.

You might also be interested in these posts:

  1. LOOP AT WHERE. Vs LOOP.IF. in ABAP
  2. Upload File from Application Server into ABAP Internal Table in SAP
  3. ABAP Program for Table Maintenance in SAP
  4. Pass different Internal Tables to a Subroutine in ABAP
  5. SAP ABAP Transferring Internal Table Contents to a file on the Application Server

One Response to “Best way to Loop At ‘Standard’ Internal Table in ABAP”
himanshu Posted on October 25, 2011 at 12:23 PM

its cool…….plz continue..to post such things…….god bless u

Post a Comment