From: Joe Conway <mail@joeconway.com>
To: sergio.fantinel@lnl.infn.it
Cc: pgsql-sql@postgresql.org
Subject: Re: PL/pgSQL multidimension (matrix) array in function
Date: Mon, 20 Sep 2004 21:44:02 -0700
Message-ID: <414FB192.5060100@joeconway.com> (raw)
In-Reply-To: <4149C90D.60806@lnl.infn.it>
References: <4149C90D.60806@lnl.infn.it>
Sergio Fantinel wrote:
> I found how to use, inside a PL/pgSQL function, a two-dimensions array
> (matrix).
> There is a limitation: the number of the 'columns' of the matrix is
> fixed at declaration time (in DECLARE section) and you need to manually
> initialize all the elements in the first 'row' of the matrix.
You should use '{}' to initialize the array to empty. See below for an
example:
CREATE OR REPLACE FUNCTION testarray (integer, integer) RETURNS SETOF
integer[] AS'
DECLARE
n alias for $1; -- number of rows is passed as argument
i INTEGER;
j integer;
k alias for $2; -- matrix columns number
a integer[];
begin
for i in 1..n loop
a := ''{}''; -- create empty array
for j in 1..k loop
a := a || i;
return next a;
end loop;
end loop;
return;
end;
'LANGUAGE 'plpgsql';
regression=# select * from testarray(2,3);
testarray
-----------
{1}
{1,1}
{1,1,1}
{2}
{2,2}
{2,2,2}
(6 rows)
HTH,
Joe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: mail@joeconway.com, sergio.fantinel@lnl.infn.it
Subject: Re: PL/pgSQL multidimension (matrix) array in function
In-Reply-To: <414FB192.5060100@joeconway.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox