【问题标题】:create a trigger that will update the attribute on a table when rows are inserted, deleted, or updated创建一个触发器,该触发器将在插入、删除或更新行时更新表上的属性
【发布时间】:2017-03-10 04:12:39
【问题描述】:

我有两张桌子

  • 行(inv_number line_num p_code line_units line_price)
  • 产品(P_code P_desit _P_indate P_qoh P_min P_price p_dicount v_code P_min_order P_Reorder

我必须创建一个触发器,它会自动更新给定产品的产品表中的属性 p_qoh。

因此,每次我从行表中插入、更新或删除一行时,它都会减少、更新或增加 P_QOH 属性。

这就是我目前所拥有的一切

create or replace trigger TRG_LINE_PRODUCT
after insert update or delete on TBL_CH08_PRODUCT
for each row
begin 

【问题讨论】:

  • 不确定您的问题是什么,但手册中有很多示例可供您学习。

标签: sql oracle


【解决方案1】:

首先要了解在哪里创建触发器,本例中目标表是PRODUCT,但是你软件的逻辑条件告诉我们事件的源表是LINE。

触发器如下所示:

CREATE OR REPLACE Trigger TRG_LINE_PRODUCT
  After Insert Or Update Or Delete On LINE
  Referencing Old As Old New As New
  For Each Row
Declare
Begin
  If inserting Then
    If :New.p_code Is Not Null Then
      Update PRODUCT
      Set    p_qoh = p_qoh + 1
      Where  p_code = :New.p_code;
    End If;
  ElsIf updating Then
    If Nvl(:Old.p_code, 'XyZ@') <> Nvl(:New.p_code, 'XyZ@') Then
      Update PRODUCT
      Set    p_qoh = p_qoh - 1
      Where  p_code = :Old.p_code;    
      Update PRODUCT
      Set    p_qoh = p_qoh + 1
      Where  p_code = :New.p_code;    
    End If;
  ElsIf deleting Then
    If :Old.p_code Is Not Null Then
      Update PRODUCT
      Set    p_qoh = p_qoh - 1
      Where  p_code = :Old.p_code;    
    End If;
  End If;
Exception
  When Others Then
    Null;
End TRG_LINE_PRODUCT;

触发器知道哪个事件“触发”,分别有关键字插入、更新和删除。

对于更新的情况,如果将 :new 和 :old 数据与此关键字进行比较,则可以通过一种形式来了解列目标是否发生变化。

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 2011-05-05
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 2020-08-23
    • 2012-04-13
    相关资源
    最近更新 更多