Is there any way to rollback and exit psql script on error?

I have a psql script that looks like this:

-- first set of statements
begin
sql statement;
sql statement;
sql statement;
exception
    when others then
    rollback
    write some output
    (here I want to exit the entire script and not continue on to the next set of statements)
end
/
-- another set of statements
begin
sql statement;
sql statement;
sql statement;
exception
    when others then
    rollback
    write some output
    (here I want to exit the entire script and not continue)
end
/
... and so on

Is it possible to exit the script and stop processing the rest of the script?

+5
source share
3 answers

Place the following lines at the top of your file:

WHENEVER OSERROR EXIT ROLLBACK
WHENEVER SQLERROR EXIT ROLLBACK

... and make sure you have RAISE;exception handlers at the end.

+8
source

I try to use raise_application_error when I want to stop the execution and pass the error code / message to the calling script:

http://www.java2s.com/Tutorial/Oracle/0480__PL-SQL-Programming/UsingRAISEAPPLICATIONERROR.htm

+2
source

... PL/SQL GOTO, , script.
:

GOTO the_end;
[rest of script here]
<<the_end>>
+1

All Articles