I have a simple PostgreSQL script and try to run the for loop with cursor to fetch rows as pagination:
begin;
declare curs cursor for select id from postgres.core.security_group order by id asc;
fetch 4 from curs;
commit;
Working fine, but when I add a for loop to it, won't work:
CREATE OR REPLACE FUNCTION postgres.core.cursor_selection()
RETURNS SETOF varchar AS
$func$
DECLARE
curs cursor for select * from postgres.core.security_group;
_modules varchar; -- assuming data type integer
BEGIN
FOR _modules IN SELECT * FROM postgres.core.security_group ORDER BY id limit 10
LOOP
RETURN NEXT _modules;
END LOOP;
END
$func$ LANGUAGE plpgsql;
SELECT postgres.core.cursor_selection();
I have the loop not working properly and not showing more data other than the first 10 records. How do I get the data as a set of 10s on each page? Much appreciated.
LIMIT 10, so you will always get the same ten first rows. Declare the cursor outside the function and only fetch from it in the function.