Thread: ERROR: array subscript out of range

ERROR: array subscript out of range

From
chinchu2005
Date:
Hello all,
Need ur help.I dont know wats wrong with the following fucntion.
create or replace function twoarray() returns setof integer as
'
declare
i integer;
j integer;
a integer[][];
begin
for i in 1..10 loop
for j in 1..2 loop
a[i][j]:=i*j;
return next a[i][j];
end loop;
end loop;
return;
end;
'
language 'plpgsql';

when i execute the following statement i get an error saying 'array
subscript out of range'

select * from twoarray();
ERROR: array subscript out of range
CONTEXT: PL/pgSQL function "twoarray" line 8 at assignment

Help me,please
--
View this message in context: http://www.nabble.com/ERROR%3A-array-subscript-out-of-range-tp22992481p22992481.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: ERROR: array subscript out of range

From
Tom Lane
Date:
chinchu2005 <chinchu2005@gmail.com> writes:
> declare
> i integer;
> j integer;
> a integer[][];
> begin
> for i in 1..10 loop
> for j in 1..2 loop
> a[i][j]:=i*j;

This isn't going to work --- it implies dynamically resizing the array,
and plpgsql isn't smart enough to do that for a multidimensional array.
Do you actually need a 2-D array here?  If so you'll have to initialize
it to the right dimensions to start with, eg with
    a := '{{1,2,3,4,5,6,7,8,9,10},{1,2,3,4,5,6,7,8,9,10}}';

            regards, tom lane

Re: ERROR: array subscript out of range

From
Alvaro Herrera
Date:
chinchu2005 escribió:
>
> Hello all,
> Need ur help.I dont know wats wrong with the following fucntion.
> create or replace function twoarray() returns setof integer as
> '
> declare
> i integer;
> j integer;
> a integer[][];
> begin
> for i in 1..10 loop
> for j in 1..2 loop
> a[i][j]:=i*j;

We just had this question on the spanish list.  Somebody was kind enough
to write the answers down in the Wiki:
http://wiki.postgresql.org/wiki/Matrices_Multidimensionales_con_funciones

It is all in spanish, but the code examples should be helpful
nonetheless.

--
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.