I have a question about EXECUTE IMMEDIATE. It does not need an explicit commit to commit DML statements. Is it possible to force it not to commit until explicitly told to do so?
The EXECUTE IMMEDIATE command does not perform an implicit commit after every iteration. To verify this, I'll show a simple test case. First, I'll create a small table:
ORA9I SQL> create table test (id number); Table created.Then, I'll run through a loop to insert into this table some values. After the loop is complete, I'll rollback all EXECUTE IMMEDIATE INSERT statements:
ORA9I SQL> DECLARE 2 counter NUMBER; 3 BEGIN 4 FOR counter IN 1..5 LOOP 5 EXECUTE IMMEDIATE 'INSERT INTO test VALUES ('||counter||')'; 6 END LOOP; 7 ROLLBACK; 8 END; 9 / PL/SQL procedure successfully completed. ORA9I SQL> select * from test; no rows selectedAs you can see, my one ROLLBACK statement rolled back all five INSERT statements. This means that there was no implicit commit done with the EXECUTE IMMEDIATE command. There is an implicit commit performed with DDL statements, not DML statements. Many times, the EXECUTE IMMEDIATE is used for dynamically executing DDL statements, so this is where your confusion may have come in. And you do not need EXECUTE IMMEDIATE for most dynamic DML statements. I can simply write the above PL/SQL block as follows:
ORA9I SQL> DECLARE 2 counter NUMBER; 3 BEGIN 4 FOR counter IN 1..5 LOOP 5 INSERT INTO test VALUES(counter); 6 END LOOP; 7 COMMIT; 8 END; 9 / PL/SQL procedure successfully completed. ORA9I SQL> select * from test; ID ---------- 1 2 3 4 5However, you will need EXECUTE IMMEDIATE if you do something like dynamically change the table name in the INSERT statement.
For More Information
- Dozens more answers to tough Oracle questions from Brian Peasland are available.
- The Best Oracle Web Links: tips, tutorials, scripts, and more.
- Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical Oracle and SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig Deeper on Oracle database design and architecture
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.