Is something like this possible?
FOR row_var IN SELECT * FROM my_table LOOP
-- ...
IF is_last_row THEN
-- do something...
END IF;
END LOOP
The only thing that goes into my mind now is to select a count of the rows and compare it with row_number()
in the loop.
There is a very cheap and simple way. Your row variable row_var
still holds the last row after the loop ends. Just use it then:
FOR row_var IN
SELECT * FROM my_table ORDER BY ???
LOOP
-- do something for every row here
END LOOP;
-- do something with row_var for the last row here
Aside from that, there is often a more efficient solution with plain SQL, depending on the undisclosed details of your use case ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With