创建ORACLE触发器(练习/学习目的)

时间:2013-10-08 03:16:52

标签: sql oracle oracle11g triggers

我正在尝试创建一个触发器,当我对'customer'表进行更新时会触发该触发器。我希望触发器将cust_no,cust_name,contact和sysdate写入另一个名为Customer_Changelog的表。

我似乎没有太多运气......

这是我到目前为止所做的:

CREATE OR REPLACE TRIGGER Customer_Up_Tr
AFTER UPDATE OF cust_name, contact ON N_Customer
FOR EACH ROW
WHEN (OLD.contact <> 1 AND NEW.contact = 1 OR OLD.cust_name <> 1 AND NEW.cust_name = 1)
DECLARE
lv_cust_no NUMBER;
lv_cust_name VARCHAR(20);
lv_contact VARCHAR(20);
BEGIN
SELECT cust_no INTO lv_cust_no FROM N_Customer WHERE cust_no = :OLD.cust_no;
SELECT cust_name INTO lv_cust_name FROM N_Customer WHERE cust_name = :OLD.cust_name;
SELECT contact INTO lv_contact FROM N_Customer WHERE contact = :OLD.contact;
INSERT INTO
Customer_changelog (lv_cust_no,lv_cust_name, lv_contact) VALUES (cust_no, cust_name, contact);
END;
/

它给我带来了这些错误:

9/1    PL/SQL: SQL Statement ignored
10/86  PL/SQL: ORA-00984: column not allowed here

如果我能指出正确的方向,我会很感激。

1 个答案:

答案 0 :(得分:4)

您可以像这样编写触发器,

create or replace 
TRIGGER Customer_Up_Tr
     AFTER UPDATE OF cust_name, contact ON N_Customer
     FOR EACH ROW
     WHEN (OLD.contact <> 1 AND NEW.contact = 1 OR OLD.cust_name <> 1 AND NEW.cust_name = 1)
BEGIN     
     INSERT INTO customer_changelog (cust_no, cust_name, contact) VALUES (:OLD.cust_no, :OLD.cust_name, :OLD.contact);
END;