Is COMMIT required after every EXECUTE IMMEDIATE?
Asked Answered
I

2

16

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?

Incompetent answered 6/12, 2013 at 20:34 Comment(1)
You should only commit once when your transaction is finished - so I guess that would be at the end.Hippo
S
11

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).

Solitta answered 6/12, 2013 at 20:55 Comment(0)
U
6

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.

Unhealthy answered 18/6, 2014 at 10:26 Comment(1)
Some statements are "final" - final in the sense that you do not need a "COMMIT" or "ROLLBACK". However, for execute immediate it depends on the statement that you executed. For example, if you execute an "UPDATE" or an "INSERT" or a "DELETE" you will need a "COMMIT" to commit the transaction. As noted in one of the earlier responses, COMMIT and ROLLBACK are session attributes - this means that you are COMMITing or ROLLBACKing all uncommitted transactions up to that point. Using an "EXECUTE IMMEDIATE" for a dynamic execution does NOT eliminate the need for "COMMIT" or "ROLLBACK".Unhealthy

© 2022 - 2024 — McMap. All rights reserved.