【问题标题】:Creating an ORACLE Trigger (Practice / Study purposes)创建 ORACLE 触发器(练习/学习目的)
【发布时间】:2013-10-14 19:59:08
【问题描述】:

我正在尝试创建一个触发器,该触发器在对我的“客户”表进行更新时触发。我希望触发器将 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

如果我能指出正确的方向,我将不胜感激。

【问题讨论】:

    标签: sql oracle oracle11g triggers


    【解决方案1】:

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

    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;
    

    【讨论】:

    • 看起来不错,谢谢。我如何才能将 sysdate 添加到 INSERT 语句中?
    • 就这样添加,INSERT INTO customer_changelog (lv_cust_no,lv_cust_name, lv_contact, o_date) VALUES (:OLD.cust_no, :OLD.cust_name, :OLD.contact, sysdate);
    • 我仍然收到 PL/SQL: ORA-00904: "SYSDATE": invalid identifier
    • INSERT INTO customer_changelog (cust_no, cust_name, contact, sysdate) VALUES (:OLD.cust_no, :OLD.cust_name, :OLD.contact, sysdate);
    • 如此明显的答案。先生,您是一位绅士和一位学者。
    猜你喜欢
    • 2013-10-15
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2013-04-06
    • 2011-12-04
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多