2

Using EXECUTE IMMEDIATE inside PL\SQL block makes the whole block commit immediately.

begin 
INSERT INTO Customer ( GUID, STATUS, NAME) VALUES (1,1,'xx');
EXECUTE IMMEDIATE 'CREATE TABLE Shop
(
 GUID NUMBER(16),
 STATUS NUMBER(1),
 NAME VARCHAR2(50 BYTE),
)
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING';
DBMS_OUTPUT.PUT_LINE('DONE:'); 
EXCEPTION -- exception handlers begin 
 WHEN OTHERS THEN -- handles all other errors 
 DBMS_OUTPUT.PUT_LINE('Error occured, rollback...'); 
 ROLLBACK;
end; 

As you realized I do not even use COMMIT. About above code,

"Insert into" statement works, but "create table" statement throws exception because there is already a table with the same name in the database.

Both I did not have any commit statement and code block fell exception and rolled back when I looked at the database I saw that insert had worked and there was a new row. It was expected that it should not have been there because there is no commit and also rollback worked..

How can I make rollback when exception occurs.

asked May 22, 2014 at 10:03
2
  • 3
    Any DDL statement will commit a pending transaction regardless whether the DDL is successful or not. Oracle does not support transactional DDL. Commented May 22, 2014 at 10:06
  • 1
    Cross post: stackoverflow.com/questions/23803601/… Commented May 22, 2014 at 11:45

1 Answer 1

3

In Oracle when you create a table or execute an alter statement there is an implied commit. You may want to create the table in a separate function returning a boolean value with "PRAGMA AUTONOMOUS_TRANSACTION" then the calling procedure would get a true/false response as to whether or not the table was created. you will then be able to commit or rollback your insert.

answered May 22, 2014 at 17:46
2
  • Any DDL statement causes an implicit commit. Not every ALTER statement, however, is DDL so not every ALTER statement does an implicit commit. For example, ALTER SESSION SET nls_date_format = ... is an ALTER statement that is not DDL and that does not implicitly commit. Commented May 22, 2014 at 18:14
  • Right it needs to be DDL, as in alter table, alter index, etc. Commented May 22, 2014 at 18:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.