PL/PgSQL在给出错误的循环内调用函数 [英] PL/PgSQL calling a function inside a loop giving error

查看:119
本文介绍了PL/PgSQL在给出错误的循环内调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The code bellow is giving error on w_add_ax_extra(1, 'k', 'v') previously it was w_add_ax_extra(some_id, kv.k, kv.v) I changed it to k, v to reproduce the same error

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

我遇到语法错误,但无法理解我所缺少的内容

I am getting Syntax Error but could not understand what I am missing

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'),它会起作用.是的,此函数返回一个整数.但我不需要将其存储在这里.强制保留返回值吗?

However If I do dummy = w_add_ax_extra(1, 'k', 'v') it works. Yes this function returns an integer. But I don't need to store it here. Is it mandatory to hold the return value ?

推荐答案

来自

39.5.2.执行没有结果的命令

[...]

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

Sometimes it is useful to evaluate an expression or SELECT query but discard the result, for example when calling a function that has side-effects but no useful result value. To do this in PL/pgSQL, use the PERFORM statement:

PERFORM query;

强调我的.您并不是通过说f();之类来调用函数,而是需要perform f();select f() into ...;:

Emphasis mine. You're not calling the function by saying something like f();, you need to perform f(); or 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;

这篇关于PL/PgSQL在给出错误的循环内调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆