Thread: arrays in pl/pgsql

arrays in pl/pgsql

From
Michael Shapiro
Date:
Is it possible in pl/pgsql to declare and then assign to arrays?
For example if I declare an array as follows
DECLARE    dn text[];

How do I assign the first element to a string (say 'a').
I have tried the following (which doesn't work)
dn[1] := \'a\';

The parser doesn't like the [ that follows dn.




Re: arrays in pl/pgsql

From
Tom Lane
Date:
Michael Shapiro <mshapiro@ncsa.uiuc.edu> writes:
> I have tried the following (which doesn't work)
>     dn[1] := \'a\';
> The parser doesn't like the [ that follows dn.

I believe Joe Conway fixed this in 7.4.  Note that you also need to
initialize the array to something, because assigning to an element
of a NULL array still yields a NULL array result.  For example:

regression=# create or replace function foo() returns text as '
regression'# declare dn text[] := \'{}\';
regression'# begin
regression'#   dn[1] := \'a\';
regression'#   return dn[1];
regression'# end' language plpgsql;
CREATE FUNCTION
regression=# select foo();foo
-----a
(1 row)
        regards, tom lane