Can you try to use the implicit for loop?
Instead of open c1;loop;fetch c1 into...;close c1;end loop;,
you can use for i in c1;loop...end loop;