How to overcome(Probably using WHILE with a local variable) when an SQL CURSOR is FETCH'ing the next parameter as a wrong one

Example with expected output

Attached is an example to understand better

Actual output