Accessing array elements in a FOR PL/pgsql loop

From: Igor Katson <descentspb(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Accessing array elements in a FOR PL/pgsql loop
Date: 2009-02-19 10:36:35
Message-ID: 499D3633.2030702@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

For each element in the array, I need to make some operation with plpgsql.
I usually use the syntax:

DECLARE
array_len int;
BEGIN
array_len := array_upper(i_array, 1);
FOR i IN 1 .. array_len
LOOP
SOME OPERATION (i_array[i])
END LOOP;

But I don't like that. Is there any built-in way to do that without
using the length, i.e like in python, e.g.
for element in array:
....
This example does not work in Postgres.

I think I need a built-in function to make a column from an array, like
in the backwards operation SELECT ARRAY(column)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2009-02-19 10:41:04 Re: When adding millions of rows at once, getting out of disk space errors
Previous Message Thomas Kellerer 2009-02-19 10:14:04 Re: postgres wish list