Re: Reading an OUT parameter out of a function call

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Stefan Keller <sfkeller(at)gmail(dot)com>
Cc: pgsql-general List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Reading an OUT parameter out of a function call
Date: 2013-02-25 17:34:09
Message-ID: CAFj8pRBJTYKoNHwEctT68=TX7Y_JH_Q3+JwPU-8oaJXqRV1Niw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello

2013/2/25 Stefan Keller <sfkeller(at)gmail(dot)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(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2013-02-25 17:37:41 Re: Reading an OUT parameter out of a function call
Previous Message Merlin Moncure 2013-02-25 17:30:47 Re: Reading an OUT parameter out of a function call