calling a plpgsql function with a composite type array argument #420
-
I know this seems like a very niche and weird thing to do, but I'm trying to do this and it's not working. For now I'm using another method, but I'm just curious whether I could make it work. create table something (
id int generated by default as identity primary key,
username varchar(64) not null unique,
age smallint
) and a type like: create type person as (
username varchar(64),
age smallint
) and a function like: create function create_person(people person[]) returns varchar(16) as $$
declare person person; begin
foreach person in array people loop
insert into something (username, age) values (person.username, person.age);
end loop;
return 'done';
end $$ language plpgsql; I know it seems weird, but in my case I have to check some conditions and also figure out some other variable to put in the third column for each of the rows in the same table but this is the gist of the problem.
I've also tried: So I got a little creative:
I should say that this time I didn't have any item with age equal to null because I got the error below:
So anyone has any idea how to do this? Or if this is even possible? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yeah, there's no good way to serialize records (yet), but you were close with your last attempt. This one should work for you ;)
It will result in a query like this, and pass the values as parameters:
|
Beta Was this translation helpful? Give feedback.
Yeah, there's no good way to serialize records (yet), but you were close with your last attempt.
This one should work for you ;)
It will result in a query like this, and pass the values as parameters: