PostgreSQL ecpg: How to call function with several out parameters

285 Views Asked by At

Suppose I have stored function 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;

What ist the idiomatic way to call this function from an ecpg program? The best I have found so far is

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

or

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

but this seems rather clumsy.

2

There are 2 best solutions below

0
Laurenz Albe On BEST ANSWER
EXEC SQL SELECT p_foo, p_bar INTO :foo, :bar FROM foobar();
0
Marcelo Diaz On

Remember null value, add indicator variables.

example:

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;