creating multi dimensional varchar/text arrays in plpgsql (postgres)
-
26-09-2019 - |
Question
i have some functionality i'd like to get from the server-side code into the database.
i can't figure out how to set the values of a multi-demensional varchar array in plpgsql.
here is an example of what i'm trying to do:
`CREATE OR REPLACE FUNCTION my_function (my_arg integer) RETURNS text[][] AS
$$
DECLARE
my_arr varchar[][];
r_row RECORD;
counter integer:= 1;
BEGIN
FOR r_row IN SELECT my_stuff1, my_stuff2 FROM my_table WHERE my_val = my_arg LOOP
my_arr[counter][1] := r_row.my_stuff1;
my_arr[counter][2] := r_row.my_stuff2;
my_arr[counter][3] := 'my_string';
END LOOP;
return my_arr;
END;
$$
LANGUAGE plpgsql;`
for the life of me, i cannot figure out how to get this to work.
any help would be great. i am using postgres 8.1 by the way (stuck at that version).
Solution
Try like this,I have tested it without any problem.
CREATE OR REPLACE FUNCTION my_function (my_arg integer) RETURNS text[][] AS
$$
DECLARE
my_arr varchar[][];
r_row RECORD;
counter integer:= 1;
BEGIN
FOR r_row IN SELECT my_stuff1, my_stuff2 FROM my_table WHERE my_val = my_arg LOOP
my_arr[counter] := array[r_row.my_stuff1,r_row.my_stuff2,'my_string'];
counter :=counter+1;
END LOOP;
return my_arr;
END;
$$
LANGUAGE plpgsql;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow