Improved version of Parallel Cursor
Problem with previous Method: The disadvantage of the code in my previous post is that we can not use it when the inner loop key fields are are independent of key fields of outer loop. i.e. The assumption was the inner loop index keeps incrementing as the outer loop index index moves ahead. But this is not the case for 90% of our code. For example let outer loop be VBPA table and inner loop be KNA1 table, in which case, the key field for VBPA will be VBELN, but the key field for KNA1 would be KUNNR. Also reader can note it is not necessary that inner loop index gets incremented for each outer loop parse.
Alternate Method: To overcome this problem, we use a READ statement with binary search before the inner loop starts. This gets the starting index of the inner loop and then rest of the logic remains same.
Reader can witness that this methods is very easy and understandable compared to previous code.
Code:
Alternate Method: To overcome this problem, we use a READ statement with binary search before the inner loop starts. This gets the starting index of the inner loop and then rest of the logic remains same.
Reader can witness that this methods is very easy and understandable compared to previous code.
Code:
sort: lt_vbpa by vbeln,
kna1 by kunnr.
loop at lt_vbpa into wa_vbpa.
read lt_kna1 into wa_kna1
with key kunnr = wa_vbpa-kunnr
binary search.
if sy-subrc = 0.
v_kna1_index = sy-tabix.
loop at lt_kna1 into wa_kna1 from v_kna1_index.
if wa_kna1-kunnr <> wa_vbpa-kunnr.
exit.
endif.
****** Your Actual logic within inner loop ******
endloop. "KNA1 Loop
endif.
endloop. " VBPA Loop
Labels: Nested Loops, Parallel Cursor, Performance, SAP