PL / SQL - 从触发器

时间:2015-11-26 19:38:53

标签: sql oracle plsql triggers

我遇到触发器细节方面的问题,特别是从触发器中访问不同类型的值并改变表错误。 (使用PL / SQL Oracle 11g)

假设我有桌子fus:

|     |  A  |  B  |  id |
|-----------------|-----|
|row 1|  0  |  1  |  0  |
|row 2|  1  |  0  |  1  |

我有触发器ro:

create or replace trigger ro
after update of A on fus
when (new.A = 1 and old.A = 0)

declare
  da fus.B%type := :new.fus.B; --how do i do this correctly?
  cnt number(2) := 0;

begin
  select sum(A)
    into cnt
    from fus
    where B = da;

end;

我有声明:

update fus
  set A = 1
  where id = 0;

显然,我得到错误,我声明'da':坏绑定变量;我只是不知道如何获得A已经更新的B的值。我还得到一个变异表错误,我试图使用复合触发器,但我仍然得到变异表错误我运行更新。老实说,我不明白为什么这是一个变异的表;我只是希望在整个更新完成后触发器触发。我在这个网站和其他网站的多个帖子上阅读了如何解决这个变异表错误,但我似乎无法弄清楚如何解决这个具体问题。

这是我的实际代码:

触发:

-- 9-5 trig:
create or replace trigger bb_discount_trg
  for update of orderplaced on bb_basket
  when (new.orderplaced = 1 and old.orderplaced = 0) -- when orderplaced switches from 0 to 1
  compound trigger

    /* shared declarations */
    cnt number(2) := 0; -- just for readability
    newShopId bb_basket.idshopper%type := :new.bb_basket.idshopper; -----> /!\ FIX /!\

  after statement is
    begin
      /* count # of orders for this shopper */
      select sum(orderplaced)
        into cnt
        from bb_basket
        where idshopper = newShopId; -----> /!\ FIX /!\

      /* store # of orders */
      disc_pkg.pv_disc_num := cnt;

      /* if cnt is multiple of 5, then discount */
      if mod(disc_pkg.pv_disc_num, 5) = 0 then
        disc_pkg.pv_disc_txt := 'Y';
      else
        disc_pkg.pv_disc_txt := 'N';
      end if;
  end after statement;

end;
/
show errors

封装

-- 9-5 pkg spec:
create or replace package disc_pkg is
  pv_disc_num number(2); -- stores number of orders
  pv_disc_txt varchar2(1); -- stores Y for discount, else no discount
end disc_pkg;

声明:

update bb_basket
  set orderplaced = 1
  where idBasket = 13;

0 个答案:

没有答案