开发者

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

开发者 https://www.devze.com 2022-12-28 03:46 出处:网络
I have a psql script that looks like this: -- first set of statements begin sql statement开发者_如何学C;

I have a psql script that looks like this:

-- first set of statements
begin
sql statement开发者_如何学C;
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?


Put the following lines at the top of your file:

WHENEVER OSERROR EXIT ROLLBACK
WHENEVER SQLERROR EXIT ROLLBACK

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


I tend to use raise_application_error when I want to halt execution and pass back a custom error code/message to the calling script:

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


Hmmm... PL/SQL does have GOTO so you could jump to a label you place right at the end of the script.
eg:

GOTO the_end;
[rest of script here]
<<the_end>>
0

精彩评论

暂无评论...
验证码 换一张
取 消