有没有办法在出错时回滚并退出psql脚本?

时间:2010-04-23 18:46:30

标签: oracle plsql

我有一个psql脚本,如下所示:

-- 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

是否可以退出脚本并停止处理脚本的其余部分?

3 个答案:

答案 0 :(得分:8)

将以下行放在文件的顶部:

WHENEVER OSERROR EXIT ROLLBACK
WHENEVER SQLERROR EXIT ROLLBACK

...并确保在异常处理程序的末尾有RAISE;

答案 1 :(得分:2)

当我想暂停执行并将自定义错误代码/消息传回给调用脚本时,我倾向于使用raise_application_error:

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

答案 2 :(得分:1)

嗯...... PL / SQL确实有GOTO所以你可以跳到你放在脚本末尾的标签。
例如:

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