更新Oracle中的触发器PL / SQL

时间:2016-05-23 19:57:27

标签: sql oracle plsql triggers

我创建了以下表格:

create table products

(
        code             varchar(9),
        group_code       varchar(9),
        price            number,  

CONSTRAINT pk_code PRIMARY KEY (code)
);

我创建了一个触发器,因为每组产品的价格不超过100美元:

create or replace trigger nomore100
before insert or update on products
for each row
declare
cursor c_total is
select group_code, sum(price) as total_price
from products
where group_code=:new.group_code
group by group_code;
v_total c_total%rowtype;
begin
for v_total in c_total loop
    if v_total.total_price+:new.price > 100 then
        raise_application_error(-20150,'No more than 100 dollars');
    end if;
end loop;
end nomore100;
/

触发器适用于插入,但是当我尝试进行更新时:

更新产品套装价格= 120,其中代码='PX1'; Oracle回归:

  

“表%s。%s正在变异,触发器/功能可能看不到它”

感谢您提出任何建议或答案,祝您有个愉快的一天!

现在我从HERE尝试以下触发器:

CREATE OR REPLACE TRIGGER compound_trigger
FOR INSERT OR UPDATE ON products
COMPOUND TRIGGER

  TYPE group_code_t   IS TABLE OF products.group_code%TYPE INDEX BY varchar2(100);
  v_group_codes          group_code_t;

     BEFORE EACH ROW IS
     BEGIN
        v_group_codes( :new.group_code ) := :new.group_code;
     END BEFORE EACH ROW;

     AFTER STATEMENT IS
        v_total NUMBER;
     BEGIN
      -- for each updated department check the restriction
      FOR code IN v_group_codes.FIRST .. v_group_codes.LAST
      LOOP
         SELECT sum(price) INTO v_total FROM products WHERE code = group_code;
         IF v_total > 100 THEN
            raise_application_error(-20123, 'ERROR MORE THAN 100 DOLLARS');
         END IF;
      END LOOP;
     END AFTER STATEMENT;

END compound_trigger;
/

但是我得到了同样的错误:

  

“表%s。%s正在变异,触发器/功能可能看不到它”

0 个答案:

没有答案