尝试创建函数时的Postgres plpgsql错误

时间:2020-04-02 21:17:42

标签: postgresql plpgsql psql postgresql-9.5

Postgres 9.5.2

获取:

ERROR:  syntax error at end of input
LINE 14: ...ast_modified_at, NEW.first_name, NEW.last_name);

对于psql shell中的这些语句,在运行$$;之后:

CREATE OR REPLACE FUNCTION customer_history_trigger() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
    NEW.last_modified_at = now();
    IF (TG_OP = 'UPDATE') THEN
        IF OLD.status IS DISTINCT FROM NEW.status or OLD.parent_id IS DISTINCT FROM NEW.parent_id
        or OLD.first_name IS DISTINCT FROM NEW.first_name or OLD.last_name IS DISTINCT FROM NEW.last_name
        or OLD.company IS DISTINCT FROM NEW.company or OLD.company_alias IS DISTINCT FROM NEW.company_alias
        THEN
            INSERT INTO customerhistory
                (customer_id, modified_at, first_name, last_name)
            VALUES
                (NEW.id, NEW.last_modified_at, NEW.first_name, NEW.last_name);
        END IF;
    ELSIF (TG_OP = 'INSERT') THEN
        INSERT INTO customerhistory
            (customer_id, modified_at, first_name, last_name)
        VALUES
            (NEW.id, NEW.last_modified_at, NEW.first_name, NEW.last_name);
    END IF;
    RETURN NEW;
END;
$$;

错误中还有一个小箭头指向行尾的分号。显然,它不喜欢此行末尾的分号:(NEW.id, NEW.last_modified_at, NEW.first_name, NEW.last_name);,但我不知道为什么。

1 个答案:

答案 0 :(得分:0)

事实证明,这与INSERT INTO customerhistoryVALUES之后的换行符有关。如果我删除了这两个换行符,而另两个换行了,那么将该语句粘贴到psql中,它将起作用。但是,它不适用于那些换行符。

如果我在同一行上打开括号,也许也可以吗?没有测试。