postgresql - Postgres - array for loop -


i writing plpgsql script.
want array contents 1 one 2 dimension array.

declare   m varchar[];   arr varchar[][] := array[['key1','val1'],['key2','val2']]; begin   m in select arr   loop     raise notice '%',m;   end loop; end; 

but above code returns

{{key1,val1},{key2,val2}} 

in 1 line. want able loop on , call function takes parameters like:

another_func(key1,val1) 

since postgresql 9.1 there convenient foreach:

do $do$ declare    m   varchar[];    arr varchar[] := array[['key1','val1'],['key2','val2']]; begin    foreach m slice 1 in array arr    loop       raise notice 'another_func(%,%)',m[1], m[2];    end loop; end $do$ 

solution older versions:

do $do$ declare    arr varchar[] := '{{key1,val1},{key2,val2}}'; begin    in array_lower(arr, 1) .. array_upper(arr, 1)    loop       raise notice 'another_func(%,%)',arr[i][1], arr[i][2];    end loop; end $do$ 

also, there no difference between varchar[] , varchar[][] postgresql type system. explain in more detail here.

the do statement requires @ least postgresql 9.0, , language plpgsql default (so can omit declaration).


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -