Many thanks to Stephan, Richard and George. When I was reading the documentation about FOR loops, I didn't realize
thatI was in the plpgsql section!
CREATE OR REPLACE FUNCTION rgio() RETURNS integer as $$
BEGIN DECLARE lv RECORD; FOR lv IN SELECT DISTINCT rg FROM ldevrg LOOP SELECT ldev FROM
ldevrgWHERE ldevrg='$lv'; END LOOP;
RETURN 1;
END;
$$ LANGUAGE plpgsql;
So I've made progress, but I'm running into the same thing.
psql:rgio.sql:16: ERROR: syntax error at or near "FOR" at character 86
psql:rgio.sql:16: LINE 6: FOR lv IN SELECT DISTINCT rg FROM ldevrg LOOP
psql:rgio.sql:16: ^
If I comment out the DECLARE statement above, I get the following:
psql:rgio.sql:16: ERROR: loop variable of loop over rows must be record or row variable at or near "LOOP" at character
129
psql:rgio.sql:16: LINE 6: FOR lv IN SELECT DISTINCT rg FROM ldevrg LOOP
psql:rgio.sql:16: ^
Ideas?
________________________________
From: pgsql-sql-owner@postgresql.org on behalf of Kevin Nikiforuk
Sent: Thu 7/20/2006 7:46 AM
To: pgsql-sql@postgresql.org
Subject: [SQL] Error when trying to use a FOR loop
Sorry if this is in the archives, but I've done a search and couldn't find anything relevant. I'm running HP's
precompiledversion of 8.1.3.1 as part of their Internet Express offering, and I can't seem to run a for loop. Here's
whatI'm seeing:
xp512-0715-0716=# FOR LV in 1..10 LOOP
xp512-0715-0716-# select * from ldevrg;
ERROR: syntax error at or near "FOR" at character 1
LINE 1: FOR LV in 1..10 LOOP
^
I'm still pretty new to postgres, but based on the documentation I'm not picking up what I'm doing wrong.
Help much appreciated.
Thanks,
Kevin
---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faq