I think it's the following snippet of code in your inner loop:


IF sa_sp_cur%NOTFOUND THEN
    v_counter_2 := v_counter_2 + 1; /* increment counter where match not found */
    EXIT;
else
    v_counter_3 := v_counter_3 + 1; /* increment counter where match found */
END IF;


You will always increment v_counter_2 even if you do find a row. First time thorugh the inner loop, you find a row and increment v_counter_3. You will restart the loop, see the no more rows are found and then increment v_counter_2.

I would suggest using %ROWCOUNT in tandem with %NOTFOUND to see if you should set v_counter_2.

HTH,

Heath