Link to home
Start Free TrialLog in
Avatar of YBSolutions
YBSolutionsFlag for Argentina

asked on

whenever sqlerror exit sql.sqlcode rollback except

I am need to run the development scripts very frequently in my development database.

I want to rollback and exit the script if any error/s except below 2.

1. ORA-02289: sequence does not exist.
2. ORA-00942: table or view does not exist.

Could you let me know how to achieve it?

It is Oracle database version 11.1.0.7
Avatar of YBSolutions
YBSolutions
Flag of Argentina image

ASKER

Just FYI,

I have tested below is working fine. It will roll back and exit.

whenever sqlerror exit sql.sqlcode rollback

But I want to ignore below errors.

1. ORA-02289: sequence does not exist.
2. ORA-00942: table or view does not exist.
You will either have to put all the 'drop' statements before the 'WHENEVER' statement on envelop each drop statement like this:
WHENEVER SQLERROR CONTINUE;

DROP TABLE myTable;

WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK

Open in new window

:p
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>But I want to ignore below errors.

If the above post does not solve the issue:  What is generating those errors?  What are you wanting to 'ignore'?

If it is DDL, that does an implicit commit and previous DML cannot be rolled back.

for example:


update some_table set col1='Y';

--this will succeed
create table bob( col1 char(1));

--this will fail
create table bob( col1 char(1));

--This does nothing
rollback;
--The update has been committed since the first DDL succeeded.
It would be great to see the actual code.  Adding to slightwv @37804120, the errors suggest you are combining object creation with data manipulation.  For example, you might see those errors if you are querying another schema's objects for which you don't have object privileges.  Is that a possible factor here?
Guys,

My script has ddl and dml both.
I know the fact told by 'slightwv' that ddl has implicit commit.

I am okay with this, How ever I want to roll back all dml (including previous to ddl).
Can you comment by keeping this in mind ?
                          OR

Can you suggest some other way/script to get it done?
Hi,

Waiting for your comment/s.
Grins, sorry, sleep time in the U.S.  I'm pleased that you follow slight's reasoning, but that begs the obvious -- you cannot roll back prior to a commit (ddl implicit).

Therefore, allow me to welcome me to your solution:  database recovery to a prior point in time / system change number:

http://www.oracle-base.com/articles/10g/Flashback10g.php
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9011.htm
http://orafaq.com/node/1847
OK, I think I understand now.  I missed your comment when I first read the question:   except below 2

I don't think you can to this in straight sqlplus.  You can create a stored procedure say, do_ddl and trap those two specific errors.

create or replace procedure do_ddl(inSQL in varchar2)
is
	err_num number;
	err_msg varchar2(255);
	table_not_found EXCEPTION;
	sequence_not_found EXCEPTION;
	PRAGMA EXCEPTION_INIT(table_not_found, -942);
	PRAGMA EXCEPTION_INIT(sequence_not_found, -2289);
begin

	execute immediate inSQL;

	exception
		when table_not_found  then
			null;
		when sequence_not_found  then
			null;

end;
/

show errors

--generate a 942 and continue
exec do_ddl('drop table jnfgugure');
--generate a 2289 and continue
exec do_ddl('drop sequence jnfgugure');
-- generate a 904 and exit
exec do_ddl('select ralph from dual');

Open in new window

elegant as always :)
I wrote below script, any comment?
Note: You can test is by connecting scott/tiger.

set errorlogging on
-- dml start from here ----
select * from dual ;

insert into salgrade values (9, 999, 9999) ;

insert into salgrade values (10, 100, 1000) ;

insert into salgrade values (11, 1111, 11111) ;

-- select test from abc ;

drop table abc ;

---dml finish here ----

declare
i int ;

begin
select count(*) into i from sperrorlog where MESSAGE not like '%ORA-00942%' ;
select count(*) into i from sperrorlog where MESSAGE not like '%ORA-02289%' ;

if i!=0 then

rollback ;

else
commit ;

end if ;
end ;
/
Now, I need to show the output at the end
if script was successful and committed  
                             OR
It encountered errors (other than ORA-00942, ORA-02289) and rolled back.

Could you help me to find out how to use
'DBMS_OUTPUT.PUT_LINE' in given condition.
Please feel free to suggest any other method if you have.
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Testing.
I wrote below query to solve it.


truncate table sperrorlog ;
set errorlogging on
----------

------ SCRIPT STARTS FROM HERE -----

...Script goes here...

----- SCRIPT ENDS HERE ------

Paste below lines after the script.

-------
declare
i int ;
begin
select count(*) into i from sperrorlog where MESSAGE not like '%ORA-00942%' and MESSAGE not like '%ORA-02289%' ;
if i!=0 then
rollback ;
else
commit ;
end if ;
end ;
/

SET LINESIZE 190
col username format a20
col timestamp format a15
col message format a100

select username, message, TO_CHAR(timestamp, 'dd-mon-yyyy hh24:mi:ss') from sperrorlog where MESSAGE not like '%ORA-00942%' and MESSAGE not like '%ORA-02289%' ;