Is there any way to detect that set returned by a query in Postgres is empty?
Namely:
FOR varname IN
select testvar from foo
LOOP
<do some stuff on the set>
END LOOP;
-- Loop finished or Set was EMPTY
IF NOT FOUND ????? THEN
<do some other stuff for empty set>
END IF;
Of course I can run the query prior to the loop and use the NOT FOUND
or introduce a counter and then check if it was 0
at the end of the loop but I was wondering if there is a way to avoid that.
(May be more appropriate for dba.SE but starting here)
If you want to use only buildin solution, then you should to use FOUND
variable. It is true
when the result was not empty or false
when result is empty. It is safe, and the value doesn't depend on statements in cycle's body.