Parallel Cursor in ABAP is a way to modify the conventional nested loop in such a way that the overall performance of the ABAP program gets improved.
Suppose we have two tables, one table has Employee basic details and another table has Employee monthly salary details. And the requirement is to print Employee Basic details with all his salaries. What a programmer will do, he will write two Loops. The first loop will get the employee basic details one by one and the other loop will get employee salary details. You can check this thing in below ABAP code:
LOOP AT zemployee_basic INTO wa_employee_basic. LOOP AT zemployee_salary INTO wa_employee_salary where employee_id = wa_employee_basic-employee_id. Write: wa_employee_basic-employee_id, wa_employee_salary-month, wa_employee_salary-amount. ENDLOOP. ENDLOOP.
The above code will work fine and will print Employee ID, Month of the Salary and Amount of the salary of each and every employee.
What if you want to find Salary Amount of one person for a particular month? You will again write the same loops to find the required details.
Also, for every new Employee ID, the second Loop still goes through the whole data.
What we conclude with this process is that, this process is neither good for printing data nor good for search operation as it wastes ample amount of time.
Parallel Cursor Concept
To curb this problem, a concept called Parallel Cursor was introduced by SAP ABAP.
According to Parallel cursor concept, we need to follow following steps:
Step 01: Sort both the tables
Step 02: Write the first loop, like the previous way itself.
Step 03: Read the second table with the key that was required to search the data. This would give the first value regarding the key and also its position in the table [SY-TABIX].
Step 04: If the above step executed successfully, it means data is there in the second table associated with the key. Hence the SY-SUBRC will be set to zero. In this step we will check the value of SY-SUBRC. If it is zero, than we will proceed.
Step 05: Assign the SY-TABIX value into a local variable. We do this step to preserve the SY-TABIX value as it changes with the loop.’
Step 06: Now it’s the time to write the second loop. This time we don’t write the WHERE condition. This time we will write FROM condition with the local variable we initialized above.
Step 07: In this step we check if the key of first table is equal to the key of second table or not. If not, then exit the loop. This stops the loop from reading non required data.
Step 08: In this step we write our actual logic or print statements. And then close both the loops.
Parallel Cursor Example
Let us implement parallel cursor algorithm in the last example we discussed.
SORT: zemployee_basic, zemployee_salary. [Step 01]
LOOP AT zemployee_basic INTO wa_employee_basic. [Step 02]
READ TABLE zemployee_salary INTO wa_temp WITH KEY employee_id = wa_employee_basic-employee_id BINARY SEARCH. [Step 03]
IF SY-SUBRC = 0. [Step 04]
lv_index = SY-TABIX. (you need to declare this variable earlier only) [Step 05]
LOOP AT zemployee_salary INTO wa_employee_salary FROM lv_index . [Step 06]
IF wa_employee_basic-employee_id <> wa_employee_salary-employee_id. [Step 07]
Write: wa_employee_basic-employee_id, wa_employee_salary-month, wa_employee_salary-amount. [Step 08]
From above steps and example, it is clear that Parallel cursor is better than conventional nested loops. Actually nested loops take O(n1*n2) time to execute both the loops while parallel cursor takes O(n1+n2) time to execute the same.