开发者

Oracle Trigger on Insert or Delete or Update

开发者 https://www.devze.com 2023-02-14 15:17 出处:网络
Trying to create an Oracle trigger that runs after a table is updated in any way. I\'ve been googling this all morning and came up with this:

Trying to create an Oracle trigger that runs after a table is updated in any way. 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.NE开发者_如何转开发W_ON_HAND);
END;
/

I'm trying to keep it to a single record per TIDE_CODE in the new table.

V_AD_ON_HAND is a view that pulls an inventory count. gb_transaction is my new table where I'm logging these events

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. There is, after all, no NEW value as the record is expunged. You can only access the :OLD values on delete.

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;" statement, add a line that says:

SHOW ERRORS TRIGGER YOUR_TRIGGER_NAME;

0

精彩评论

暂无评论...
验证码 换一张
取 消