3

I have a function in my Postgres 8.3 database that takes a "key" variable as argument.
I want to call this function for a range of key values.

I tried this, didn't work ...

BEGIN
for i IN 773..775 LOOP
  test_count(i);
end LOOP;
end;

SQL error:
ERROR: syntax error at or near "for"
LINE 2: for i IN 773..775 LOOP

4
  • 3
    didn't work is not a valid Postgres error message. Commented Jun 22, 2012 at 21:36
  • SQL error: ERROR: syntax error at or near "for" LINE 2: for i IN 773..775 LOOP Commented Jun 22, 2012 at 21:37
  • @a_horse meant you should update your question and mention the error message. If it's in the comments anyone trying to answer your question will have to read every comment to find bits and pieces of info. Commented Jun 22, 2012 at 21:39
  • As others have pointed out, this would be easier in version 9.0 or 9.1 (or 9.2, which should be released later this summer). Version 8.3 will hit end-of-life in about 8 months. When you upgrade, you should strongly consider jumping over 8.4 and 9.0 and going to 9.1 or later. Commented Jun 23, 2012 at 15:50

3 Answers 3

4

Procedural elements (typically PL/pgSQL) can only be run inside a code block - in a function or a DO command, not in plain SQL.

Also, when "calling" a function in PL/pgSQL you must care of the return value(s). If you have no further use for them, discard return values with PERFORM:

CREATE OR REPLACE FUNCTION foo()
  RETURNS void LANGUAGE plpgsql AS
$func$
BEGIN
   FOR i IN 773 .. 775
   LOOP
      PERFORM test_count(i);
   END LOOP;
END
$func$;
Sign up to request clarification or add additional context in comments.

Comments

0

PostgreSQL 8.3 cannot run anonymous procedures/ functions or create variables outside of a procedure/ function.

The DO construct was added as support for anonymous procedures as of version 9.0.

You should run your code inside a function. Because the error message you are receiving states that FOR is an unexpected keyword in a global context.

6 Comments

It can, just not that outdated version he is using.
I'm updating the answer i forgot to mention the DO construct in 9+ indeed.
@a_horse_with_no_name - Wait what version of PG does that work on?
I've already statet that in my answer as well as my comment here. The version is > 9.
@MihaiStancu: Major versions in PostgreSQL are denominated by the first two parts of the number, i.e. 8.4, 9.0, 9.1, ... Strictly speaking, there is no such thing as "version 9". Read up about PostgreSQL versioning here. The DO statement was announced in the release notes for 9.0 here.
|
0

Use a record type for your keys

DO $BODY$
DECLARE tmp_row record;
BEGIN

  FOR tmp_row IN (SELECT key from my_keys_table)
   LOOP
     PERFORM test_function(tmp_row.key);
  END LOOP;

END;
$BODY$;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.