Re: Iterate over an array in stored procedure

From: "Merlin Moncure" <mmoncure(at)gmail(dot)com>
To: "Henrik Zagerholm" <henke(at)mac(dot)se>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Iterate over an array in stored procedure
Date: 2007-06-20 02:06:10
Message-ID: b42b73150706191906t885f624vdb6bd4a046b36cca@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 6/20/07, Henrik Zagerholm <henke(at)mac(dot)se> wrote:
> Hello,
>
> I wonder how I can iterate over a sql array in a pl/perl function.
>
> I want to pass in an text[] to a function and then iterate through
> the values and run it through a perl sprintf but I can't seem to get
> it to work.
>
> Should I do a generate_series and make an internal perl array?
> If so how would I do this?

well, it's not pl/perl, but you can expand array like this:

create or replace function explode_array(in_array anyarray) returns
setof anyelement as
$$
select ($1)[s] from generate_series(1,array_upper($1, 1)) as s;
$$
language sql immutable;

There is also a undocumented internal function, _pg_expandarray, which
does something similar, iirc it's in the information_schema namespace.
Once you have it in set form, manipulation in perl should be trivial.
(there may be a better way to do this in pl/perl, i really have no
idea).

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bob Pawley 2007-06-20 02:09:31 Re: Excell
Previous Message EBIHARA, Yuichiro 2007-06-20 01:17:10 PostgreSQL Installer for Windows x64