psql - loop variable of loop over rows must be a record or row variable or list of scalar variables

Richard Knop picture Richard Knop · Apr 11, 2013 · Viewed 42.2k times · Source

Here is my simple anonymous code block:

do $$
  declare foo varchar(50) := '';
  begin
    for a in
      select a from (values('foo'), ('bar'), ('fooBar')) s(a)
    loop
      foo := a;
      print foo;
    end loop;
  end;
$$;

When I run it:

psql -f test.sql

I get this error:

psql:test.sql:11: ERROR:  loop variable of loop over rows must be a record or row variable or list of scalar variables
LINE 4:     for a in
            ^

Answer

Richard Knop picture Richard Knop · Apr 11, 2013

Solved it myself, meh:

do $$
  declare
    arow record;
    foo varchar(50);
  begin
    for arow in
      select a from (values('foo'), ('bar'), ('fooBar')) s(a)
    loop
      foo := arow.a;
      RAISE NOTICE 'Calling cs_create_job(%)', foo;
    end loop;
  end;
$$;