Oracle 在插入、删除或更新时触发

发布于 2024-10-19 20:18:49 字数 893 浏览 1 评论 0原文

尝试创建一个在表以任何方式更新后运行的 Oracle 触发器。我整个早上都在谷歌上搜索这个问题并想出了这个:

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;
/

我试图将其保留为新表中每个 TIDE_CODE 的单个记录。

V_AD_ON_HAND 是一个提取库存计数的视图。 gb_transaction 是我的新表,我在其中记录这些事件将

其与其他人的代码进行比较,它看起来应该运行,但我收到“警告:使用编译错误创建的触发器”。

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.NEW_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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2024-10-26 20:18:49

我认为问题在于删除触发器的 :new 指定。毕竟,当记录被删除时,就没有 NEW 值了。您只能在删除时访问 :OLD 值。

如果按操作对触发器进行分段,则可以执行此操作。

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;
/

顺便说一句,如果您告诉我们错误是什么,而不仅仅是您遇到了错误,通常会很有帮助。如果通过 SQL*Plus 脚本编译,则在正斜杠调用编译后的“end;”之后触发语句中,添加一行:

SHOW ERRORS TRIGGER YOUR_TRIGGER_NAME;

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文