次のような 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
これら 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;