PLS-00049:错误的绑定变量触发器

时间:2017-03-20 03:20:32

标签: oracle plsql

我试图让这个触发器工作。它的作用是,在从表及其附属表中删除数据之前执行,然后将删除的数据插入2个表(我将其命名为tb1_arch和tb2_arch)。我一直在谷歌搜索可能的修复但我真的不知道如何使这项工作

CREATE OR REPLACE TRIGGER trig
BEFORE DELETE ON tb1 FOR EACH ROW
FETCH
INSERT INTO tb1_arch VALUES (:old.OrderNum, :old.tb1Date,:old.CustomerName);
DBMS_OUTPUT.PUT_LINE('Data archived.');
END;

上面创建了触发器。但是当我添加这一行

INSERT INTO tb2_arch VALUES (:old.OrderNum, :old.ItemNum, :old.Pieces);

INSERT INTO tb1_arch VALUES之后,它给了我一个错误

"第5行出错:PLS-00049:错误的绑定变量' OLD.PIECES'"

任何帮助将不胜感激!

编辑:正如Vijayakumar先生建议的那样,我做了以下事情:

CREATE OR REPLACE TRIGGER trig
    BEFORE DELETE ON tb1 
    REFERENCING OLD AS old FOR EACH ROW
    INSERT INTO tb1_arch VALUES (:old.OrderNum, :old.tb1Date,:old.CustomerName);
    INSERT INTO tb2_arch VALUES (:old.OrderNum, :old.ItemNum, :old.Pieces);
    DBMS_OUTPUT.PUT_LINE('Data archived.');
    END

但是,我仍然遇到同样的错误。

编辑:删除了FETCH,我仍然遇到同样的问题

CREATE OR REPLACE TRIGGER trig
BEFORE DELETE ON tb1 FOR EACH ROW

INSERT INTO tb1_arch VALUES (:old.OrderNum, :old.tb1Date,:old.CustomerName);
INSERT INTO tb2_arch VALUES (:old.OrderNum, :old.ItemNum, :old.Pieces);
DBMS_OUTPUT.PUT_LINE('Data archived.');
END;

表格如下: TB1:

create table tb1 (
    OrderNum integer NOT NULL primary key,
    tb1Date date NOT NULL,
    CustomerName varchar2(50) NOT NULL,

--constraints
    CONSTRAINT tb1_uc unique (CustomerName)
);  

TB2

create table tb2 (
    OrderNum integer NOT NULL,
    ItemNum integer NOT NULL,
    Pieces integer NOT NULL,

--constraints
    CONSTRAINT tb2_fk foreign key (OrderNum) references tb1(OrderNum),
);

1 个答案:

答案 0 :(得分:1)

关键字DELIMITER $$ CREATE TRIGGER after_insert AFTER INSERT ON SampleTable FOR EACH ROW BEGIN UPDATE SampleTable SET media = '../trigger/smile1.png' WHERE flag =0; UPDATE SampleTable SET media = '../trigger/smile2.png' WHERE flag =1; UPDATE SampleTable SET media = '../trigger/smile3.png' WHERE flag =2; END$$ DELIMITER ; 应该导致此问题,将其替换为FETCH,这是正确的语法,

BEGIN
  

编辑:您使用CREATE OR REPLACE TRIGGER trig BEFORE DELETE ON tb1 FOR EACH ROW BEGIN INSERT INTO tb1 VALUES (:old.OrderNum, :old.Date,:old.CustomerName); INSERT INTO tb2_arch VALUES (:old.OrderNum, :old.ItemNum, :old.Pieces); DBMS_OUTPUT.PUT_LINE('Data archived.'); END; 引用的列名可能无效,这就是导致此错误的原因。

触发器仅适用于 ONE 表的OLD事件。但是,您可以在正文中引用多个表格。

INSERT/UPDATE/DELETEOLD将始终引用您在DDL中提到的表格NEW

以下修改后的代码可以为您提供帮助。

DELETE ON tb1

或者,您可以使用另一个触发器CREATE OR REPLACE TRIGGER trig BEFORE DELETE ON tb1 FOR EACH ROW DECLARE v_ItemNum tb2.ItemNum%TYPE; v_Pieces tb2.Pieces%TYPE; BEGIN INSERT INTO tb1_arch VALUES (:old.OrderNum, :old.Date,:old.CustomerName); /* Select the values for this item from table 2 */ SELECT ItemNum,Pieces INTO v_ItemNum,v_Pieces FROM tb2 WHERE OrderNum = :old.OrderNum; /* Insert the selected values in the table2's archive version */ INSERT INTO tb2_arch VALUES (:old.OrderNum, v_ItemNum, v_Pieces); /* Delete the entry */ DELETE from tb2 WHERE OrderNum = :old.OrderNum; DBMS_OUTPUT.PUT_LINE('Data archived.'); END; 并将数据归档到那里。