Re: Looping through arrays - Mailing list pgsql-general

From David Fetter
Subject Re: Looping through arrays
Date
Msg-id 20051104051846.GA22736@fetter.org
Whole thread Raw
In response to Looping through arrays  (Robert Fitzpatrick <lists@webtent.net>)
List pgsql-general
On Thu, Nov 03, 2005 at 06:15:08PM -0500, Robert Fitzpatrick wrote:
> I have a field with 'AA-BB-CC-DD' and I want to pull those four
> values into an array and then loop through the array inserting
> records into a table for each element. Can you someone point me to
> an example of this in pl/pgsql?

You can do it in SQL, at least in 8.0 and later :)

INSERT INTO bar(blurf)
SELECT (string_to_array('AA-BB-CC-DD','-'))[s.i] AS "foo"
FROM generate_series(
    array_lower(string_to_array('AA-BB-CC-DD','-'),1),
    array_upper(string_to_array('AA-BB-CC-DD','-'),1)
) AS s(i);

Cheers,
D
--
David Fetter david@fetter.org http://fetter.org/
phone: +1 510 893 6100   mobile: +1 415 235 3778

Remember to vote!

pgsql-general by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: Looping through arrays
Next
From: Joe Conway
Date:
Subject: Re: Looping through arrays