行级触发器对当前表操作,”ORA-04091: 表 COMPANY 发生了变化, 触发器/函数不能读它“,解决方法:
1、自动产生另外一个独立的TRANSACTION,一般需要在其中写commit,见红色内容
create or replace trigger odpaydetailTrigger
before insert or update on order_pay_detail
for each row
declare
PRAGMA AUTONOMOUS_TRANSACTION;--自动产生另外一个独立的TRANSACTION,一般需要在其中写commit
-- local variables here 一个订单 pay_reason=‘MAIN’只能有一个
detailcount int;
begin
if :new.pay_reason='MAIN' then
begin
if inserting then
begin
select count(1) into detailcount from order_pay_detail opd where opd.order_package_id=:new.order_package_id and opd.pay_reason='MAIN' AND PAY_SUC='Y';
if detailcount>=1 then
begin
raise_application_error(-20020, '重复插入类型为MAIN的支付明细');
end;
end if;
end;
end if;
if updating then
begin
--select count(*) into detailcount from order_pay_detail opd where opd.order_package_id=:new.order_package_id and opd.pay_reason='MAIN';
if (:new.pay_reason<>:old.pay_reason or :new.PAY_SUC<>:old.PAY_SUC) and :new.pay_reason='MAIN' and :new.PAY_SUC='Y' then
begin
select count(1) into detailcount from order_pay_detail opd where opd.order_package_id=:new.order_package_id and opd.pay_reason='MAIN' AND PAY_SUC='Y';
if detailcount >=1 then
begin
raise_application_error(-20020, '重复插入类型为MAIN的支付明细');
end;
end if;
end;
end if;
end;
end if;
end;
end if;
COMMIT;
end odpaydetailTrigger;