Skip to content

Instantly share code, notes, and snippets.

@schaary
Last active December 17, 2019 07:23
Show Gist options
  • Save schaary/33e219427c1459ab09e2c29295d5cc1b to your computer and use it in GitHub Desktop.
Save schaary/33e219427c1459ab09e2c29295d5cc1b to your computer and use it in GitHub Desktop.
for loop to fetch records from select in postgresql
-- An implicite cursor in PostgreSQL PLPGSQL.
--
-- The main idea is to declare a cursor in the loop header
-- and fetch the rows inside the loop
do $$
declare
rec record;
begin
for rec in
select *
from accounts
order by lastname, firstname
loop
raise notice '% - %', rec.firstname, rec.lastname;
end loop;
end
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment