psql - 循环遍历行的循环变量必须是记录或行变量或标量变量列表

时间:2013-04-11 12:54:55

标签: postgresql psql

这是我简单的匿名代码块:

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;
$$;

当我运行它时:

psql -f test.sql

我收到此错误:

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
            ^

1 个答案:

答案 0 :(得分:60)

自己解决了,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;
$$;