0

我在我的表上创建了一个触发器。当插入/更新/删除时,它会添加一个键 i,u,d 这样。我试图在另一个表上插入这个键,但给了我这样的错误:

    ERROR:  column "i" does not exist
    LINE 1: ...(operation,stamp,userid,empname,salary) VALUES('||i||', now(...
                                                                 ^
    QUERY:  SELECT dblink_exec('INSERT INTO emp_audit(operation,stamp,userid,empname,salary) VALUES('||i||', now(), user,NEW.*)')
    CONTEXT:  PL/pgSQL function process_emp_audit() line 14 at PERFORM


    ********** Error **********

    ERROR: column "i" does not exist
    SQL state: 42703
    Context: PL/pgSQL function process_emp_audit() line 14 at PERFORM

我不知道为什么它给我这个错误。这是我的触发功能

-- Function: process_emp_audit()

-- DROP FUNCTION process_emp_audit();

CREATE OR REPLACE FUNCTION process_emp_audit()
  RETURNS trigger AS
$BODY$
    BEGIN
    PERFORM dblink_connect('dbname=ekders port=5432 user=****** password=*****');

        IF (TG_OP = 'DELETE') THEN
        PERFORM dblink_exec('INSERT INTO emp_audit "D", now(), user, OLD.*;');
        PERFORM dblink_disconnect();
        RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
        PERFORM dblink_exec('INSERT INTO emp_audit "U", now(), user, NEW.*;');
        PERFORM dblink_disconnect();
        RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
        PERFORM dblink_exec('INSERT INTO emp_audit(operation,stamp,userid,empname,salary) VALUES('||i||', now(), user,NEW.*)');
        PERFORM dblink_disconnect();
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION process_emp_audit()
  OWNER TO postgres;
4

1 回答 1

3

您的代码正在尝试将i不存在的 value 连接到字符串。我怀疑你真正想做的是逃避单引号。以下应该工作:

PERFORM dblink_exec('INSERT INTO emp_audit(operation,stamp,userid,empname,salary) VALUES(''i'', now(), user,NEW.*)');
于 2013-10-31T11:42:26.707 回答