Is COMMIT required after every EXECUTE IMMEDIATE?

风格不统一 提交于 2020-01-13 09:12:11

问题


I have multiple EXECUTE IMMEDIATE commands within one oracle procedure.

EXECUTE IMMEDIATE 'DELETE  FROM tbl1'; 
EXECUTE IMMEDIATE 'INSERT INTO tbl1...'; 
COMMIT;
EXECUTE IMMEDIATE 'DELETE  FROM tbl3'; 
EXECUTE IMMEDIATE 'INSERT INTO tbl3 ...'; 
COMMIT;
EXECUTE IMMEDIATE 'DELETE  FROM tbl4'; 
EXECUTE IMMEDIATE 'INSERT INTO tbl4 ...';
COMMIT; 

Do I need all of these COMMIT, or just at the end of the procedure?


回答1:


The only times that you're really forced to commit, other thasn at the end of a business transaction, are:

  1. When executing DDL: the DDL execution is wrapped in a pair of implicit commits.
  2. After direct path insert: the table cannot be read until the insert is committed.

As horsey comments, the correct point to commit at is when the business transaction is complete. Otherwise, you need to be writing yourself some code to detect and fix partially completed and commited transactions that have left the database is a logically inconsistent state (eg. An INVOICE record exists without any INVOICE_DETAIL records).




回答2:


Commit is not required after every EXECUTE IMMEDIATE. Certain statements do NOT require a commit; for example, if you truncate a table with TRUNCATE. The truncation is done and there is no need for a commit. no ROLLBACK either. You need to know that COMMIT and ROLLBACK are session attributes. All uncommitted work within the current transaction are committed or rolled back - not just the statement executed by the EXECUTE IMMEDIATE.



来源:https://stackoverflow.com/questions/20433082/is-commit-required-after-every-execute-immediate

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