列“i”不存在

时间:2013-10-31 11:37:09

标签: sql postgresql

我在我的桌子上创建了一个触发器。当插入/更新/删除时,它会添加一个键,我,你喜欢这样。 我正试图在另一张桌子上插入这个键但是给我这样的错误:

    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;

1 个答案:

答案 0 :(得分:2)

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

PERFORM dblink_exec('INSERT INTO emp_audit(operation,stamp,userid,empname,salary) VALUES(''i'', now(), user,NEW.*)');