Thread: Generating table rows from arrays

Generating table rows from arrays

From
Florian Weimer
Date:
Is there a convenient way to turn an array into table rows?  For
example, I've got an array like {1, 2, 3} and would like to insert
rows:
 ('aaa', 1) ('aaa', 2) ('aaa', 3)

The first row is constant.  I could write a loop with PL/pgsql, I
guess, but I wonder if there are better options nowadays.

--
Florian Weimer                <fweimer@bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99


Re: Generating table rows from arrays

From
Richard Huxton
Date:
Florian Weimer wrote:
> Is there a convenient way to turn an array into table rows?  For
> example, I've got an array like {1, 2, 3} and would like to insert
> rows:
> 
>   ('aaa', 1)
>   ('aaa', 2)
>   ('aaa', 3)
> 
> The first row is constant.  I could write a loop with PL/pgsql, I
> guess, but I wonder if there are better options nowadays.

The smallest function I've seen is Merlin Moncure's here:

http://people.planetpostgresql.org/merlin/index.php?/archives/4-fun-with-arrays.html


--   Richard Huxton  Archonet Ltd


Re: Generating table rows from arrays

From
"Pavel Stehule"
Date:
Hello

se own unpack sql function

http://www.pgsql.cz/index.php/PostgreSQL_SQL_Tricks#Array_to_table

Regards
Pavel Stehule

2008/7/17 Florian Weimer <fweimer@bfk.de>:
> Is there a convenient way to turn an array into table rows?  For
> example, I've got an array like {1, 2, 3} and would like to insert
> rows:
>
>  ('aaa', 1)
>  ('aaa', 2)
>  ('aaa', 3)
>
> The first row is constant.  I could write a loop with PL/pgsql, I
> guess, but I wonder if there are better options nowadays.
>
> --
> Florian Weimer                <fweimer@bfk.de>
> BFK edv-consulting GmbH       http://www.bfk.de/
> Kriegsstraße 100              tel: +49-721-96201-1
> D-76133 Karlsruhe             fax: +49-721-96201-99
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

Re: Generating table rows from arrays

From
Florian Weimer
Date:
* Richard Huxton:

>> The first row is constant.  I could write a loop with PL/pgsql, I
>> guess, but I wonder if there are better options nowadays.
>
> The smallest function I've seen is Merlin Moncure's here:
>
> http://people.planetpostgresql.org/merlin/index.php?/archives/4-fun-with-arrays.html


Ah, neat trick.  Thanks.

--
Florian Weimer                <fweimer@bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99