IDの配列(posts
)を受け取り、
それらすべてを別のテーブル(offer
)に挿入する関数を作成しようとしています。後でさらに処理するために
、新しく作成された行(offers
)のIDが必要です。
使用してエラーが発生します WITH
create function foo (post_ids text[])
returns text[]
as $$
with
offer_ids as (insert into app.offer (user_email, post_id)
values ('[email protected]', unnest(foo.post_ids))
returning id),
...
select array_agg(id) from offer_ids;
$$ language sql security definer;
エラー
ERROR: more than one row returned by a subquery used as an expression
から複数の行を返しWITH
、後で使用するにはどうすればよいですか?
エラーはCTEの部分ではなく、INSERTの部分にあると思います。unnest
SELECTステートメントの一部として使用する必要があるなどの戻り関数を設定します。
with offer_ids as (
insert into app.offer (user_email, post_id)
select '[email protected]', f.pid
from unnest(foo.post_ids) as f(pid)
returning id
),
...
select array_agg(id)
from offer_ids;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加