简体   繁体   English

Oracle插入,删除或更新时触发

[英]Oracle Trigger on Insert or Delete or Update

Trying to create an Oracle trigger that runs after a table is updated in any way. 尝试创建以任何方式更新表后运行的Oracle触发器。 I've been googling this all morning and came up with this: 我整个上午一直在搜索,并提出了以下建议:

CREATE OR REPLACE TRIGGER gb_qty_change
AFTER UPDATE OR INSERT OR DELETE ON F_ITEM_STORE
FOR EACH ROW
DECLARE
  v_qty V_AD_ON_HAND%rowtype;
  v_isbn TD_ITEM_DESCRIPTION.TD_IDENTIFIER%type;
BEGIN
    delete from gb_transaction where gb_tide = :new.ITST_ITEM_TIDE_CODE;
    select TD_IDENTIFIER INTO v_isbn from TD_ITEM_DESCRIPTION where TD_TIDE = :new.ITST_ITEM_TIDE_CODE;
    select * INTO v_qty from V_AD_ON_HAND where ITST_ITEM_TIDE_CODE = :new.ITST_ITEM_TIDE_CODE;
    insert into gb_transaction(gb_tide, gb_isbn, gb_used_on_hand, gb_new_on_hand)
      values(:new.ITST_ITEM_TIDE_CODE, v_isbn, v_qty.USED_ON_HAND, v_qty.NEW_ON_HAND);
END;
/

I'm trying to keep it to a single record per TIDE_CODE in the new table. 我正在尝试在新表中按每个TIDE_CODE将其保存为一条记录。

V_AD_ON_HAND is a view that pulls an inventory count. V_AD_ON_HAND是提取库存数量的视图。 gb_transaction is my new table where I'm logging these events gb_transaction是我记录这些事件的新表

Comparing it to other peoples code it looks like it should run but I'm getting "Warning: Trigger created with compilation errors." 与其他人的代码相比,它看起来应该可以运行,但是我得到“警告:触发器因编译错误而创建”。

The problem, I believe is with the :new designations for a delete trigger. 我认为问题在于删除触发器的:new名称。 There is, after all, no NEW value as the record is expunged. 毕竟,随着记录的删除,没有新值。 You can only access the :OLD values on delete. 您只能在删除时访问:OLD值。

if you section the trigger by operation, you can do this. 如果按操作划分触发器,则可以执行此操作。

CREATE OR REPLACE TRIGGER gb_qty_change 
AFTER UPDATE OR INSERT OR DELETE ON F_ITEM_STORE 
FOR EACH ROW 
DECLARE   
   v_qty V_AD_ON_HAND%rowtype;   
   v_isbn TD_ITEM_DESCRIPTION.TD_IDENTIFIER%type; 
BEGIN 
  IF INSERTING or UPDATING then
    ... insert your existing code
  ELSE
    ... do something similar with the :old values for the deleting case
  end if;
END;
/

Incidentally, it is generally helpfull if you tell us WHAT the error is, not just that you had one. 顺便说一句,如果您告诉我们错误是什么,而不仅仅是您有一个错误,通常对您有所帮助。 If compiling via SQL*Plus script, after the forward slash call to compile the trigger after the "end;" 如果通过SQL * Plus脚本进行编译,请在正斜杠调用之后在“ end;”之后编译触发器。 statement, add a line that says: 语句,添加一行内容:

SHOW ERRORS TRIGGER YOUR_TRIGGER_NAME; 显示错误,触发您的_TRIGGER_NAME;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM