0

I tried to append an one dimensional array to a two dimensional array using array_append in psql function. I defined count_values as output by out count_values integer[][], cout_value as variable in declare section by count_value integer[], and tried to use count_values := array_append(count_values, count_value), but the error function array_append(integer[], integer[]) does not exist poped up.

 if ...
 else
 count_value:= array_append(count_value, 0);
 end if;
 index:= index + 1;
 end loop;
 
 count_values:= array_append(count_values, count_value);
 end loop;
...
asked Dec 17, 2021 at 16:00
1
  • "An array can also be constructed by using the functions array_prepend, array_append, or array_cat. The first two only support one-dimensional arrays, but array_cat supports multidimensional arrays. " (postgresql.org/docs/13/arrays.html) Commented Dec 21, 2021 at 14:30

1 Answer 1

0

See this fiddle

create function f() returns integer[][]
as $$
declare
 count_value integer[] = '{5,6}';
 count_values integer[][] = '{{1,2},{3,4}}';
begin
 return Array_cat(count_values,count_value);
end; $$
language plpgsql;

or, starting with an empty array,

create function f() returns integer[][]
as $$
declare
 count_value integer[] = '{5,6}';
 count_values integer[][] ;
 new_cvs integer[][];
begin
 if count_values is null
 then new_cvs = array_cat(ARRAY[]::integer[][],ARRAY[count_value]);
 else new_cvs = Array_cat(count_values,count_value);
 end if ;
 return new_cvs;
end; $$
language plpgsql;

see another fiddle

answered Dec 18, 2021 at 22:40
7
  • The problem is that, my two dimensional array will be empty at first beginning, and psql seems could not recognize that it is two dimensional Commented Dec 21, 2021 at 11:17
  • Then there is the possibility of telling postgresql that your array is two dimensional. Have you tried that? Commented Dec 21, 2021 at 11:56
  • Made the example a little smarter. Commented Dec 21, 2021 at 12:37
  • Am I wrong with the cursor statements? Commented Dec 21, 2021 at 12:39
  • Can we try solving one problem at a time? Commented Dec 21, 2021 at 12:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.