PostgreSQL ecpg:如何使用多个输出参数调用函数

PostgreSQL ecpg: How to call function with several out parameters

假设我存储了函数 foobar:

create or replace function foobar(
  out p_foo varchar,
  out p_bar varchar
)
returns record as $func$
begin
  p_foo := 'foo';
  p_bar := 'bar';
end;
$func$ language plpgsql;

从 ecpg 程序调用此函数的惯用方法是什么? 到目前为止我发现的最好的是

EXEC SQL SELECT (foobar()).p_foo, (foobar()).p_bar into :foo,:bar;

EXEC SQL SELECT (y).p_foo, (y).p_bar into :foo,:bar from (select foobar() y) x;

但这看起来很笨拙。

EXEC SQL SELECT p_foo, p_bar INTO :foo, :bar FROM foobar();

记住空值,添加指标变量。

示例:

EXEC SQL BEGIN DECLARE SECTION;
  :foo_ind short;
EXEC SQL END DECLARE SECTION;

EXEC SQL SELECT p_foo, p_bar INTO :foo :foo_ind, :bar :bar_ind FROM foobar();

foo  = (foo_ind < 0) ? 0:foo;