Instaltiating an ARRAY within a function

Lists: pgsql-general
From: "Abraham, Danny" <danny_abraham(at)bmc(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Instaltiating an ARRAY within a function
Date: 2007-12-12 12:48:18
Message-ID: E9DE7963E5EA6546B42A979EC28B4D0136B28ED5@hou-ex-02.adprod.bmc.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

===========================================

CREATE OR REPLACE FUNCTION arr( inout x varchar[] )
AS
$Z$
DECLARE
i integer;
BEGIN
select ARRAY['Danny','Eissam','Moshe'] into x;
end;
$Z$ LANGUAGE 'plpgsql' VOLATILE;

===========================================

CREATE OR REPLACE FUNCTION callarr()
returns integer
AS
$Z$
DECLARE
x varchar[6];
BEGIN
perform arr(x);
RAISE NOTICE 'x[1]=%',x[1];
return 0;
end;
$Z$ LANGUAGE 'plpgsql' VOLATILE;

===========================================

select callarr();
NOTICE: x[1]=<NULL> ??? Should have been DANNY

Should it work?

Thanks

Danny


From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "Abraham, Danny" <danny_abraham(at)bmc(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Instaltiating an ARRAY within a function
Date: 2007-12-12 13:48:04
Message-ID: 162867790712120548h284fc461tcbf6c76a84a0fc1c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello

problem is elsewhere. PostgreSQL doesn't support by ref variables. In
your sample you have to do:

CREATE OR REPLACE FUNCTION callarr()
returns integer
AS
$Z$
DECLARE
x varchar[6];
BEGIN
x := arr(x); <-------!!!!!!!!!
RAISE NOTICE 'x[1]=%',x[1];
return 0;
end;
$Z$ LANGUAGE 'plpgsql' VOLATILE;

Pavel

On 12/12/2007, Abraham, Danny <danny_abraham(at)bmc(dot)com> wrote:
> ===========================================
>
> CREATE OR REPLACE FUNCTION arr( inout x varchar[] )
> AS
> $Z$
> DECLARE
> i integer;
> BEGIN
> select ARRAY['Danny','Eissam','Moshe'] into x;
> end;
> $Z$ LANGUAGE 'plpgsql' VOLATILE;
>
> ===========================================
>
> CREATE OR REPLACE FUNCTION callarr()
> returns integer
> AS
> $Z$
> DECLARE
> x varchar[6];
> BEGIN
> perform arr(x);
> RAISE NOTICE 'x[1]=%',x[1];
> return 0;
> end;
> $Z$ LANGUAGE 'plpgsql' VOLATILE;
>
> ===========================================
>
> select callarr();
> NOTICE: x[1]=<NULL> ??? Should have been DANNY
>
>
> Should it work?
>
> Thanks
>
> Danny
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>


From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Abraham, Danny *EXTERN*" <danny_abraham(at)bmc(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Instaltiating an ARRAY within a function
Date: 2007-12-12 16:00:39
Message-ID: D960CB61B694CF459DCFB4B0128514C2AE0A1B@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Danny Abraham wrote:
> ===========================================
>
> CREATE OR REPLACE FUNCTION arr( inout x varchar[] )
> AS
> $Z$
> DECLARE
> i integer;
> BEGIN
> select ARRAY['Danny','Eissam','Moshe'] into x;
> end;
> $Z$ LANGUAGE 'plpgsql' VOLATILE;
>
> ===========================================
>
> CREATE OR REPLACE FUNCTION callarr()
> returns integer
> AS
> $Z$
> DECLARE
> x varchar[6];
> BEGIN
> perform arr(x);
> RAISE NOTICE 'x[1]=%',x[1];
> return 0;
> end;
> $Z$ LANGUAGE 'plpgsql' VOLATILE;
>
> ===========================================
>
> select callarr();
> NOTICE: x[1]=<NULL> ??? Should have been DANNY
>
>
> Should it work?

Not the way you wrote it.
You are confused by output parameters which do not work the way one
might expect. They are just a simple syntax for composite return types.

CREATE FUNCTION arr(INOUT x varchar[])
is synonymous to
CREATE FUNCTION arr(x varchar[]) RETURNS varchar[]

So your example should work if you replace

PERFORM arr(x);
with
x := arr(x);

Yours,
Laurenz Albe