I have a a cursor loop that’s building a string by concatenating the contents of a table together, using code along these lines:
OPEN cur_t;
LOOP
FETCH cur_t INTO v_texttoadd;
v_string := v_string || v_texttoadd;
EXIT WHEN cur_t%notfound;
END LOOP;
The problem is, of course, that the last item gets added twice because the system runs through it once more before realising that there’s nothing more to find.
I tried playing around with something like
OPEN cur_t;
WHILE cur_t%found;
LOOP
FETCH cur_t INTO v_texttoadd;
v_string := v_string || v_texttoadd;
END LOOP;
But that didn’t seem to return anything at all.
What kind of syntax should I be using so that each row only appears in the resulting string once?
You can try this:
This works because %notfound is set when FETCH is executed and there aren’t any more rows to fetch. In your example you checked %notfound after the concatenation and as a result, you had the duplicate in the end.