Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Heiko.

Asked: June 15, 2018 - 11:17 am UTC

Last updated: June 18, 2018 - 6:26 am UTC

Version: 12

Viewed 1000+ times

You Asked

Hi there, I want my SQL*Plus-Scripts to do a ROLLBACK on EXIT when there is no explicit COMMIT (or ROLLBACK) in the Skript. So i am using the AUTOCOMMIT and EXITCOMMIT settings of SQL*Plus.

According to the description in the SQL*Plus Reference Manual in section SET EXITCOMMIT the Exit Behavior is ROLLBACK if AUTOCOMMIT is OFF, EXITCOMMIT is OFF and EXIT has no COMMIT clause.

But in fact the data is commited. Did I missunderstood this functionality or is it maybe a bug in SQL*Plus?

Here is an example, which ends in unexpectedly commiting the inserted record to the database.

SET PAGESIZE 1000 NEWPAGE NONE
SET LINESIZE 5000 WRAP OFF

SET ECHO ON FEEDBACK ON

SET AUTOCOMMIT OFF
SET EXITCOMMIT OFF

WHENEVER SQLERROR EXIT 8 ROLLBACK;

INSERT INTO TAB1 SELECT MAX(I) + 1, 'Test COMMIT' FROM TAB1;

SELECT * FROM TAB1 ORDER BY 1;

SHOW AUTOCOMMIT 
SHOW EXITCOMMIT

--ROLLBACK

EXIT 0


BTW If I omit the return code 0 then a ROLLBACK happens! But i need those return codes in the scripting context.

Regards Heiko

and Connor said...

Exit has two "modes"

1) Just plain "EXIT"
2) EXIT with 2 options (the return code and the transaction intent)

So the moment you pass back a return code, you have dropped into "mode 2"

And the *default* for the transaction intent for mode 2 is commit, which is why you are seeing what you are seeing.

You can think of it SQLPlus saying: "Hey you have me some parameters for EXIT, so I'm assuming you're taking control here"

So you'd want to include an explict rollback parameter in there.

Is this answer out of date? If it is, please let us know via a Comment