Looking for a way to insert a list of records based on an array of UUIDs. Here's my example code:
CREATE OR REPLACE FUNCTION "AddGroupUsers" (
"@OrganizationID" UUID,
"@GroupID" UUID,
"@UserIDs" UUID[]
)
RETURNS viud AS
$func$
BEGIN
FOR index IN "@UserIDs" LOOP
INSERT INTO
"UserGroups" (
"UserID",
"GroupID",
"OrganizationID"
)
VALUES (
"@UserID"[index],
"@GroupID",
"@OrganizationID"
);
END LOOP;
END;
$func$ LANGUAGE PLPGSQL;
Obviously doesn't work, lol.
I want to be able to call:
SELECT "AddGroupUsers"(
'cb6e96db-73db-4b07-811f-c54b61d09fa4',
'451a9ab7-02f6-4f63-bb87-80ad531ab490'
array(
'451a9ab7-02f6-4f63-bb87-80ad531ab490',
'451a9ab7-02f6-4f63-bb87-80ad531ab491',
'451a9ab7-02f6-4f63-bb87-80ad531ab492',
'451a9ab7-02f6-4f63-bb87-80ad531ab493',
'451a9ab7-02f6-4f63-bb87-80ad531ab494'
)::uuid[]
);
As a side note I have a unique key constraint that ensures only one record for a UserID and GroupID every exist. If the second array value breaks that rule will the whole query fail and how can I ignore it to ensure the rest of the values get inserted?