PostgreSQL过程:“没有运算符匹配给定的名称和参数类型”

时间:2019-04-01 18:35:58

标签: sql postgresql

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

我在程序中哪里出错了?

1 个答案:

答案 0 :(得分:3)

DELETE查询中,EMPLOYEE NOT IN (EMPLOYEES)不正确。 IN不是在这里“取消嵌套”数组,您最终将varcharvarchar[]进行了比较。

您可以尝试将其替换为

EMPLOYEE NOT IN (SELECT e FROM unnest(EMPLOYEES) u (e))

“手动”取消嵌套数组,

NOT EMPLOYEES @> ARRAY[EMPLOYEE]

使用数组包含运算符或

EMPLOYEE <> ALL (EMPLOYEES)

使用ANY,其中数组自动“取消嵌套”。 (可能还有更多。)