Re: Error when trying to use a FOR loop

From: "Kevin Nikiforuk" <Kevin(dot)Nikiforuk(at)Acrodex(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Error when trying to use a FOR loop
Date: 2006-07-21 15:26:21
Message-ID: 6EA72AC41F81D242A38AAF505523A94D022DF4@kappa.acrodex.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Many thanks to Stephan, Richard and George. When I was reading the documentation about FOR loops, I didn't realize that I 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 ldevrg WHERE 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(at)postgresql(dot)org on behalf of Kevin Nikiforuk
Sent: Thu 7/20/2006 7:46 AM
To: pgsql-sql(at)postgresql(dot)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 precompiled version of 8.1.3.1 as part of their Internet Express offering, and I can't seem to run a for loop. Here's what I'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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message A. Kretschmer 2006-07-21 15:39:20 Re: Error when trying to use a FOR loop
Previous Message Aaron Bono 2006-07-21 15:07:30 Re: System catalog table privileges