ERROR: array subscript out of range

Started by chinchu2005about 17 years ago3 messagesgeneral
Jump to latest
#1chinchu2005
chinchu2005@gmail.com

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.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: chinchu2005 (#1)
Re: ERROR: array subscript out of range

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

#3Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: chinchu2005 (#1)
Re: ERROR: array subscript out of range

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.