Why doesn't this rollback work? [message #235742] |
Mon, 07 May 2007 16:57 |
Catch33
Messages: 3 Registered: May 2007
|
Junior Member |
|
|
Hello all,
I'm a complete newbie to Oracle and am trying to demonstrate a simple rollback, yet it does not work. Code and output is below. Any help would be greatly appreciated.
DECLARE
row_count INTEGER := 0;
table_has_more_than_0_rows EXCEPTION;
BEGIN
/* Create table but don't add any rows to it. Note "EXECUTE IMMEDIATE"
must be used for DDL statements within PL/SQL code. */
EXECUTE IMMEDIATE 'CREATE TABLE my_table (my_int INTEGER)';
/* Select # of rows (which will be zero) from new table and store in row_count. */
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM my_table' INTO row_count;
DBMS_OUTPUT.PUT_LINE('Table has this many rows: '||row_count);
COMMIT;
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('Inserting 3 rows into table...');
EXECUTE IMMEDIATE 'INSERT INTO my_table VALUES(1)';
EXECUTE IMMEDIATE 'INSERT INTO my_table VALUES(2)';
EXECUTE IMMEDIATE 'INSERT INTO my_table VALUES(3)';
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM my_table' INTO row_count;
DBMS_OUTPUT.PUT_LINE('Table has this many rows: '||row_count);
/* Throw exception if # of rows is more than 0. */
IF row_count>0 THEN
RAISE table_has_more_than_0_rows;
END IF;
EXECUTE IMMEDIATE 'DROP TABLE my_table';
EXCEPTION
/* Handle exceptions. */
WHEN table_has_more_than_0_rows THEN
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('Attempting to rollback to 0 rows...');
ROLLBACK;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Table has this many rows: '||row_count);
EXECUTE IMMEDIATE 'DROP TABLE my_table';
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Some unknown exception was caught.');
EXECUTE IMMEDIATE 'DROP TABLE my_table';
END;
.
run;
===============
OUTPUT IS HERE:
===============
Table has this many rows: 0
Inserting 3 rows into table...
Table has this many rows: 3
Attempting to rollback to 0 rows...
Table has this many rows: 3
PL/SQL procedure successfully completed.
[Updated on: Mon, 07 May 2007 17:04] Report message to a moderator
|
|
|
|
Re: Why doesn't this rollback work? [message #235744 is a reply to message #235743] |
Mon, 07 May 2007 17:30 |
Catch33
Messages: 3 Registered: May 2007
|
Junior Member |
|
|
Thanks much anacedent,
I did not know that about "EXECUTE IMMEDIATE". Originally, I was not using that for the DML statements, but I continually ran into "PL/SQL: ORA-00942: table or view does not exist" when I just had this:
INSERT INTO my_table VALUES(1);
Changing it to use the "EXECUTE IMMEDIATE" got rid of those errors. I don't know why it can't find the table I just created (unless I use "EXECUTE IMMEDIATE"). Any ideas on that?
Thanks again!
|
|
|
|
Re: Why doesn't this rollback work? [message #235747 is a reply to message #235745] |
Mon, 07 May 2007 18:19 |
Catch33
Messages: 3 Registered: May 2007
|
Junior Member |
|
|
Right on - thanks. I was creating the table on the fly because this is just a test script. And I did notice earlier that if I didn't drop the table, the code would run fine the second time through. It just seems that when you create a table in PL/SQL using "EXECUTE IMMEDIATE", the table not really visible unless you continue using "EXECUTE IMMEDIATE". For my test purposes, I'll just create separate scripts to create and drop the table.
(EDITED)
Jeez - I'm such a yo-yo. I forgot to select COUNT(*) into row-count prior to my last output to the screen. That was probably the only problem to begin with. But the points about bad design are well-taken! Thanks for the advice.
[Updated on: Mon, 07 May 2007 18:35] Report message to a moderator
|
|
|
|
|
Re: Why doesn't this rollback work? [message #235781 is a reply to message #235748] |
Tue, 08 May 2007 00:32 |
gold_oracl
Messages: 129 Registered: July 2006 Location: Westborough, MA
|
Senior Member |
|
|
First of all, I don’t understand your question clearly. I'm presuming that you are getting 3 rows even after rollback. Correct me if I’m wrong.
As far as this code concerns it behaving absolutely right. In the Exception section you rollback the transaction, hence all the data would go out. But after that you are just displaying the local variable that you had assigned after the insertion. So obviously your row_count will have 3 and when you print it in the exception you will get 3 only not 0.
Thanks,
Thangam
|
|
|
|