pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Declaring arrays in plpgsql functions
4+ messages / 2 participants
[nested] [flat]

* Declaring arrays in plpgsql functions
@ 2002-03-25 10:04 Daniel Lundin <daniel@helena-daniel.se>
  2002-03-25 19:37 ` Re: Declaring arrays in plpgsql functions Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Daniel Lundin @ 2002-03-25 10:04 UTC (permalink / raw)
  To: pgsql-sql

I'm trying to convert rows from a table into a two dimensional array, and
thought I could do so in a plpgsql function, but I can't declare the return
variable as an array:

create function reservationIds(int4)
  returns text[][] as '
    declare
      paramOriginalResNr        alias for $1;

      rs                        record;

      id_src                    text[][];

    begin
      for rs in
        select
          src,
          id
        from
          ReservationId
        where
          originalResNr = paramOriginalResNr
      loop
        text[array_dim(text) + 1][0] := rs.src;
        text[array_dim(text) + 1][1] := rs.id;
      end loop;

      return id_src;
    end;
  '
language 'plpgsql';

, but when I select the function the parser complains: 'ERROR:  parse error at
or near "["'. It seems to be the id_src declaration that's not correct; the
returns text[][] works.

I've tried text[2][30] to the same effect.

/Daniel




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Declaring arrays in plpgsql functions
  2002-03-25 10:04 Declaring arrays in plpgsql functions Daniel Lundin <daniel@helena-daniel.se>
@ 2002-03-25 19:37 ` Tom Lane <tgl@sss.pgh.pa.us>
  2002-03-25 19:41   ` Re: Declaring arrays in plpgsql functions Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2002-03-25 19:37 UTC (permalink / raw)
  To: Daniel Lundin <daniel@helena-daniel.se>; +Cc: pgsql-sql

Daniel Lundin <daniel@helena-daniel.se> writes:
> I'm trying to convert rows from a table into a two dimensional array, and
> thought I could do so in a plpgsql function, but I can't declare the return
> variable as an array:

The declaration is fine (at least it works for me, in 7.2).  The trouble
is with:

>         text[array_dim(text) + 1][0] := rs.src;
>         text[array_dim(text) + 1][1] := rs.id;

plpgsql doesn't support assigning to array elements :-(.  (Even if it
did, you couldn't use array_dims() like that --- array_dims() returns
a string.)

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Declaring arrays in plpgsql functions
  2002-03-25 10:04 Declaring arrays in plpgsql functions Daniel Lundin <daniel@helena-daniel.se>
  2002-03-25 19:37 ` Re: Declaring arrays in plpgsql functions Tom Lane <tgl@sss.pgh.pa.us>
@ 2002-03-25 19:41   ` Tom Lane <tgl@sss.pgh.pa.us>
  2002-03-25 20:16     ` Re: Declaring arrays in plpgsql functions Daniel Lundin <daniel@helena-daniel.se>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2002-03-25 19:41 UTC (permalink / raw)
  To: Daniel Lundin <daniel@helena-daniel.se>; +Cc: pgsql-sql

I wrote:
> plpgsql doesn't support assigning to array elements :-(.

Drat, forgot a point I intended to make:

You might have better luck working in pltcl; it has better support for
arrays than plpgsql does.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Declaring arrays in plpgsql functions
  2002-03-25 10:04 Declaring arrays in plpgsql functions Daniel Lundin <daniel@helena-daniel.se>
  2002-03-25 19:37 ` Re: Declaring arrays in plpgsql functions Tom Lane <tgl@sss.pgh.pa.us>
  2002-03-25 19:41   ` Re: Declaring arrays in plpgsql functions Tom Lane <tgl@sss.pgh.pa.us>
@ 2002-03-25 20:16     ` Daniel Lundin <daniel@helena-daniel.se>
  0 siblings, 0 replies; 4+ messages in thread

From: Daniel Lundin @ 2002-03-25 20:16 UTC (permalink / raw)
  To: pgsql-sql

On mån, mar 25, 2002 at 02:41:49 -0500, Tom Lane wrote:
> I wrote:
> > plpgsql doesn't support assigning to array elements :-(.
> 
> Drat, forgot a point I intended to make:
> 
> You might have better luck working in pltcl; it has better support for
> arrays than plpgsql does.
> 
There's no chance with plperl, or have I understood it correctly, that plperl
doesn't support any way of doing selects?

> 			regards, tom lane
> 



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2002-03-25 20:16 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-03-25 10:04 Declaring arrays in plpgsql functions Daniel Lundin <daniel@helena-daniel.se>
2002-03-25 19:37 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-03-25 19:41   ` Tom Lane <tgl@sss.pgh.pa.us>
2002-03-25 20:16     ` Daniel Lundin <daniel@helena-daniel.se>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox