Re: Selecting from table into an array var

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Postgres User <postgres(dot)developer(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Selecting from table into an array var
Date: 2009-12-19 05:53:00
Message-ID: b42b73150912182153v76ccc07frb5322e05bbef9d9c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Dec 18, 2009 at 11:35 PM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> 2009/12/19 Postgres User <postgres(dot)developer(at)gmail(dot)com>:
>> Hi,
>>
>> I'm trying to write a very simple function statement to select a
>> single integer field from a table and save it into an int array. For
>> some reason I can't seem to find the correct syntax:
>>
>> CREATE TABLE sample (
>>    id  integer
>> );
>>
>> and then within a function:
>>
>> my_array int[];
>> my_array = SELECT ARRAY(id) FROM sample;
>>
>>
>> This syntax and variations of it don't work.  Can anyone show me the
>> correct approach?
>>
>
> Hello
>
> please try SELECT ARRAY(SELECT id FROM sample) or SELECT array_agg(id)
> FROM sample - if you have 8.4

yup:

array() vs array_agg() vs array[]...which to use?

*) use array[] when building list of scalar values
*) use array_agg when aggregating (you need to group by something)
*) use array() everywhere else

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2009-12-19 06:00:16 Re: PL/Perl Performance Problems
Previous Message Pavel Stehule 2009-12-19 04:35:34 Re: Selecting from table into an array var