plpgsql: calling a function with 2 OUT parameters

marco picture marco · May 14, 2012 · Viewed 24.2k times · Source

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

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


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

Answer

vyegorov picture vyegorov · May 14, 2012

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

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

SELECT * FROM get_test() INTO xx, yy;