Best Technique to use Nested Loop in ABAP Programming


Nested Loops in ABAP


If ITAB1 has n1 entries and ITAB2 has n2 entries, the time needed for the nested loop with the straightforward algorithm is O(n1 * n2), whereas the parallel cursor approach takes only O(n1 + n2) time.

The above parallel cursor algorithm assumes that ITAB2 contains only entries also contained in ITAB1.  If this assumption does not hold, the parallel cursor algorithm gets slightly more complicated, but its performance characteristics remain the same.

Straightforward nested loop 


LOOP AT ITAB1 INTO WA1.
  LOOP AT ITAB2 INTO WA2
                WHERE K = WA1-K.
    " ...
  ENDLOOP.

ENDLOOP.

More sophisticated loop: parallel cursors 

I = 1.
LOOP AT ITAB1 INTO WA1.
  LOOP AT ITAB2 INTO WA2 FROM I.
    IF WA2-K <> WA1-K.
      I = SY-TABIX.
      EXIT.
    ENDIF.
    " ...
  ENDLOOP.
ENDLOOP.

For more Tutorials, visit ABAP Tutorials, Tips & Tricks and Certification Questions

No comments:

Post a Comment