How to use COMMIT in Procedure called by trigger

狂风中的少年 提交于 2019-12-23 21:39:08

问题


I have the below trigger(mytrg) which calls a procedure(myproc) that will update table2 if any insert in table1. I have "COMMIT" statement in the procedure after the data is updated in table2. But when there is an insert in table1, i get the below error.

Error report:
SQL Error: ORA-04092: cannot COMMIT in a trigger
ORA-06512: at "myschema.myproc", line 63
ORA-06512: at "myschema.mytrg", line 2
ORA-04088: error during execution of trigger 'myschema.mytrg'
04092. 00000 -  "cannot %s in a trigger"
*Cause:    A trigger attempted to commit or rollback.
*Action:   Rewrite the trigger so it does not commit or rollback.

**Trigger:**                                                                                        
create or replace
trigger mytrg
after insert on table1
for each row
begin
myschema.myproc(:new.ID, :new.NAME, :new.TYPE_CODE, :new.LANGUAGE);
end;

Need to know how to commit the update.

Thanks


回答1:


You can not have a COMMIT inside a trigger. Your UPDATE will be committed as soon as the INSERT to the table1 is committed.

But to achieve what you want you can use an autonomous transaction. For example,

CREATE OR REPLACE TRIGGER mytrg
AFTER INSERT ON table1 FOR EACH ROW

DECLARE    
   PRAGMA AUTONOMOUS_TRANSACTION;    
BEGIN    
   myschema.myproc(:new.ID, :new.NAME, :new.TYPE_CODE, :new.LANGUAGE);    
   COMMIT;    
END;


来源:https://stackoverflow.com/questions/23295294/how-to-use-commit-in-procedure-called-by-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!