代码之家  ›  专栏  ›  技术社区  ›  dtc

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

  •  5
  • dtc  · 技术社区  · 15 年前

    我的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 回复  |  直到 13 年前
        1
  •  7
  •   Vadim K.    15 年前

    在文件顶部放置以下行:

    WHENEVER OSERROR EXIT ROLLBACK
    WHENEVER SQLERROR EXIT ROLLBACK
    

    RAISE; 在异常处理程序的末尾。

        2
  •  2
  •   aw crud    15 年前

    当我想停止执行并将自定义错误代码/消息传回调用脚本时,我倾向于使用raise\u application\u error:

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

        3
  •  1
  •   hamishmcn    15 年前

    六羟甲基三聚氰胺六甲醚。。。PL/SQL确实有 GOTO 所以你可以跳转到一个标签,你把它放在脚本的末尾。
    如:

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