Re: Reading an OUT parameter out of a function call - Mailing list pgsql-general

From Pavel Stehule
Subject Re: Reading an OUT parameter out of a function call
Date
Msg-id CAFj8pRBJTYKoNHwEctT68=TX7Y_JH_Q3+JwPU-8oaJXqRV1Niw@mail.gmail.com
Whole thread Raw
In response to Reading an OUT parameter out of a function call  (Stefan Keller <sfkeller@gmail.com>)
List pgsql-general
Hello

2013/2/25 Stefan Keller <sfkeller@gmail.com>:
> Hi,
>
> I have a simple void function:
>
> CREATE OR REPLACE FUNCTION myfn(myparam OUT int)
> AS $$
> BEGIN
>   pnr := 1;
> END;
> $$ LANGUAGE plpgsql;
>
> How do I access myparam?
> I thought this should work with 9.1/9.2: SELECT (myfn()).myparam;
> Or inside another function?
>

you cannot access to out parameters outside function - because they
doesn't exist - postgresql cannot pass parameters by ref.

your example is exactly same as int returning function - you can use
it in plpgsql

variable := myfn(); -- variable is scalar int type

if function has more out parameters, then return type is record type.

CREATE OR REPLACE FUNCTION public.f1(a integer, b integer, OUT c
integer, OUT d integer)
 RETURNS record
 LANGUAGE plpgsql
AS $function$
begin
  c := a + b;
  d := c * 2;
end;
$function$

postgres=# select f1(10,20);
   f1
---------
 (30,60)
(1 row)

postgres=# select * from f1(10,20);
 c  | d
----+----
 30 | 60
(1 row)

create or replace function foo()
returns void as $$
declare r record;
begin
  r := f1(10,20);
  raise warning 'c=%, d=%', r.c, r.d;
end;
$$ language plpgsql;
CREATE FUNCTION
postgres=# select foo();
WARNING:  01000: c=30, d=60
 foo
-----

(1 row)

Regards

Pavel Stehule




> Yours, Stefan
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general

pgsql-general by date:

Previous
From: Merlin Moncure
Date:
Subject: Re: Reading an OUT parameter out of a function call
Next
From: Adrian Klaver
Date:
Subject: Re: Reading an OUT parameter out of a function call