I want to do the insert in all cases, but I only want to return ID if my_array (type varchar[])only has one element.
I've tried something like this:
with x_array_items as (
select id from unnest(my_array) as id
)
insert into table (name, description)
select name, description
from other_table ot,
x_array_items xa
where ot.id = xa.id
returning (case when array_length(my_array, 1) = 1 is false then id else null end) into my_variable;
But I'm getting a query returned more than one row - is there a solution? Thank you!
Even if you return NULL in your
RETURNINGclause, that will still be more than one NULLs if theINSERTinserts more than one row, which causes the error.You could use more
WITHclauses to do what you want: