我有一个 plpgsql 函数,如:
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;
我的问题是这一行:
c2 in select * from unnest(string_to_array(c1.j_options,', '))
我运行的示例查询例如:
select unnest(string_to_array('1.0, 1.2',', '));
返回 2 行:
1. 1.0
2. 1.2
我需要遍历这两行,但不确定这个 unnest 语句的返回类型应该是什么,或者它应该如何在声明部分中声明。
运行脚本时出现的错误:
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_...
从下面的答案我得到以下错误
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,', '))
我不明白为什么这在脚本中不起作用。它承认那c1.j_options
是bytea
。
我修改后的脚本位是:
for c2 in
select from unnest(string_to_array(c1.j_options,', '))
loop
raise notice '%', c2;
end loop;