PL / PgSQL在循环内调用函数给出错误

时间:2012-04-09 04:06:24

标签: postgresql plpgsql

下面的代码错误w_add_ax_extra(1, 'k', 'v')以前是w_add_ax_extra(some_id, kv.k, kv.v)我将其更改为k, v以重现相同的错误

declare
  kv record;
begin
  -- Lines skipped
  for kv in select * from (select (each(extras)).*) as f(k,v) loop
    raise notice 'key=%,value=%',kv.k,kv.v;
    w_add_ax_extra(1, 'k', 'v');
  end loop;
  -- Lines Skipped
end

我收到语法错误但无法理解我缺少的内容

ERROR:  syntax error at or near "w_add_ax_extra"
LINE 1: w_add_ax_extra(1, 'k', 'v')

但是,如果我dummy = w_add_ax_extra(1, 'k', 'v')它可行。是的,这个函数返回一个整数。但我不需要在这里存储它。是否必须保留返回值?

1 个答案:

答案 0 :(得分:6)

来自fine manual

  

<强> 39.5.2。执行无结果的命令

     

[...]

     

有时评估表达式或SELECT查询但丢弃结果很有用,例如当调用具有副作用但没有有用结果值的函数时。要在PL / pgSQL中执行此操作,请使用PERFORM语句:

PERFORM query;

强调我的。您没有通过说f();之类的内容来调用该函数,您需要perform f();select f() into ...;

for kv in select * from (select (each(extras)).*) as f(k,v) loop
    raise notice 'key=%,value=%',kv.k,kv.v;
    perform w_add_ax_extra(1, 'k', 'v');
end loop;