在PostgreSQL
数据库中,我创建了如下过程:
CREATE OR REPLACE PROCEDURE creator(ID uuid, EMPLOYEES VARCHAR[]) AS $FUNCTION$
BEGIN
DELETE FROM SURVEYS_EMPLOYEES_RELATIONSHIP
WHERE SURVEY_ID = ID
AND EMPLOYEE NOT IN (EMPLOYEES);
--
INSERT INTO SURVEYS_EMPLOYEES_RELATIONSHIP (SURVEY_ID, EMPLOYEE)
SELECT SURVEY_ID ID, EMPLOYEE FROM UNNEST(ARRAY[EMPLOYEES]) EMPLOYEE
ON CONFLICT ON CONSTRAINT unique_key
DO NOTHING;
END;
$FUNCTION$ LANGUAGE plpgsql;
正如您在此过程中所看到的,我尝试先输入DELETE
的一些条目,然后再输入INSERT
的其他条目。我这样称呼这个程序:
CALL creator('99c89a24-fff2-4cbc-a542-b1e956a352f9', ARRAY['NNogerbek@gmail.com', 'IKim@gmail.com'])
由于某种原因,它会引发错误:
SQL Error [42883]: ERROR: operator does not exist: character varying <> character varying[]
No operator matches the given name and argument types. You might need to add explicit type casts.
PL/pgSQL function creator(uuid,character varying[]) line 3 at SQL statement
我在程序中哪里出错了?
答案 0 :(得分:3)
在DELETE
查询中,EMPLOYEE NOT IN (EMPLOYEES)
不正确。 IN
不是在这里“取消嵌套”数组,您最终将varchar
与varchar[]
进行了比较。
您可以尝试将其替换为
EMPLOYEE NOT IN (SELECT e FROM unnest(EMPLOYEES) u (e))
“手动”取消嵌套数组,
NOT EMPLOYEES @> ARRAY[EMPLOYEE]
使用数组包含运算符或
EMPLOYEE <> ALL (EMPLOYEES)
使用ANY
,其中数组自动“取消嵌套”。 (可能还有更多。)