postgresql plpgsql:调用带有 2 个 OUT 参数的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10584002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:29:48  来源:igfitidea点击:

plpgsql: calling a function with 2 OUT parameters

postgresqlplpgsql

提问by marco

I'm trying to fetch to values from a plpgsql function with 2 OUT paramenters but I have some problem.

我正在尝试从带有 2 个 OUT 参数的 plpgsql 函数中获取值,但我遇到了一些问题。

These are the functions:

这些是函数:

CREATE OR REPLACE FUNCTION get_test(OUT x text, OUT y text)
AS $$
BEGIN
   x := 1;
   y := 2;
END;
$$  LANGUAGE plpgsql;
----------------------------------------------------------------

CREATE OR REPLACE FUNCTION get_test_read()
RETURNS VOID AS $$
DECLARE
   xx text;
   yy text;
BEGIN

   SELECT get_test() INTO xx, yy;

   RAISE INFO 'x: <%>', xx;
   RAISE INFO 'y: <%>', yy;

END;
$$  LANGUAGE plpgsql;


The output of the command:

命令的输出:

select get_test_read();

INFO: x: <(1,2)

INFO: y: <>

get_test_read


选择 get_test_read();

信息:x:<(1,2)

信息:y:<>

get_test_read


So both the values go to the first parameter. I cannot find some example on how to call a function like this.

所以这两个值都转到第一个参数。我找不到一些关于如何调用这样的函数的例子。

回答by vyegorov

As you have 2 OUTparams, your function will return a record.

由于您有 2 个OUT参数,因此您的函数将返回一个record

In order to get all values you should use function as the source of your data and put it into the FROMclause like this:

为了获得所有值,您应该使用函数作为数据源并将其放入FROM子句中,如下所示:

SELECT * FROM get_test() INTO xx, yy;