I have a plpgsql function like:
DO
$$
DECLARE
c_id c.id%TYPE;
j_text c.j_options%TYPE;
j_option varchar;
c1 c%ROWTYPE;
begin
CREATE TYPE my_row_type AS (c2 TEXT);
for
--c1 in select c.j_options, c.id from c c
c1 in select * from c
loop
c_id = c1.id;
for
c2 in select * from unnest(string_to_array(c1.j_options,', '))
loop
raise notice 'Value: %, %', c_id, c2.j_options;
end loop;
end loop;
END
$$ language plpgsql;
My issue is this line:
c2 in select * from unnest(string_to_array(c1.j_options,', '))
The sample query I run for eg:
select unnest(string_to_array('1.0, 1.2',', '));
returns 2 rows:
1. 1.0
2. 1.2
I need to loop over these two rows but am not sure what the type of the return of this unnest statement should be or how it should be declared in the declare section.
The error I get when running the script:
ERROR: loop variable of loop over rows must be a record or row variable or
list of scalar variables
LINE 18: c2 in select * from unnest(string_to_array(c1.j_...
From the answer below I get the following error
ERROR: function string_to_array(bytea, unknown) does not exist
LINE 1: select from unnest(string_to_array(c1.j_options,', '))
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: select from unnest(string_to_array(c1.j_options,', '))
I don't understand why this would not work in the script. It recognizes that c1.j_options
is bytea
.
My amended script bit was:
for c2 in
select from unnest(string_to_array(c1.j_options,', '))
loop
raise notice '%', c2;
end loop;