Reputation: 191
can someone tell me how to initialize a multidimensional array in plpgsql? Thanks.
Upvotes: 3
Views: 9231
Reputation: 461
post in relation : PostgreSQL: ERROR: array subscript out of range
Example:
DO $$
declare
aa integer[][];
begin
aa := array_fill(0, array[5,3]);
raise notice 'aa: %', aa;
for c1 in 1..5 loop
for c2 in 2..3 loop
aa[c1][c2] := c1*c2;
end loop;
end loop;
raise notice 'aa: %', aa;
end; $$
Upvotes: 0
Reputation: 45795
pavel=# select array_fill(0, ARRAY[2,2]); array_fill ─────────────── {{0,0},{0,0}} (1 row) pavel=# select array_fill('a'::text, ARRAY[2,2,2]); array_fill ─────────────────────────────── {{{a,a},{a,a}},{{a,a},{a,a}}} (1 row)
Upvotes: 1
Reputation: 127086
CREATE OR REPLACE FUNCTION foo()
RETURNS text[]
LANGUAGE plpgsql
AS
$$
DECLARE
var text[][];
BEGIN
var := array[['1', 'a'],['2', 'b']];
RETURN var;
END;
$$;
Test:
SELECT foo();
Upvotes: 11