【问题标题】:How to return error code in case plsql compilation error from sqlplus如何在sqlplus的plsql编译错误的情况下返回错误代码
【发布时间】:2011-12-26 03:11:12
【问题描述】:

我使用“WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;”在我的 plsql 脚本中使用它们在 shell 脚本中。这工作正常:

echo exit | sqlplus user/pass@XE  @normal.sql && echo "boo"

执行脚本并打印“boo” 这也很好用:

echo exit | sqlplus user/pass@XE  @bad.sql && echo "boo"

“嘘”没有打印出来。

但万一不好是:

WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;

create or replace
PACKAGE TESTING
IS
function boo (co_id number) return varchar2;
END;
/

create or replace
PACKAGE BODY TESTING
is
end;

这显然是错误的 - 没有返回错误代码并且打印了“boo”。 如何从 sqlplus 脚本返回 plsqsl 编译错误代码?

【问题讨论】:

    标签: oracle scripting plsql sqlplus


    【解决方案1】:

    您需要从输出中解析它们。 Unix 错误代码在 0 到 255 的范围内,其中隐藏着各种掩码和信号内容。所以不能在 unix 错误码中记录 oracle 错误号。

    所以基本上你需要让你的 sql 脚本包含show errors 语句。但是您不希望在其中使用 WHENEVER 语句,因为这会在打印错误之前出错。例如bad.sql 将是

    create or replace
    PACKAGE TESTING
    IS
    function boo (co_id number) return varchar2;
    END;
    /
    show errors
    
    create or replace
    PACKAGE BODY TESTING
    is
    end;
    /
    show errors
    

    那么你的shell脚本应该是这样的:

    ERR_OUT="$( sqlplus rdbds/rdbds@XE  < bad.sql | egrep '^(ORA|PLS)-' )"
    if [ -n "$ERR_OUT" ]
    then
        echo "Errors in SQL:"
        echo "$ERR_OUT"
    else
        echo boo
    fi
    

    【讨论】:

    • 谢谢,这就是我的想法,但我希望有更好的方法。
    • 与其解析“显示错误”的输出,不如在 sql 中解析 user_errors 或 all_errors 表的内容,如下所示:forums.oracle.com/forums/thread.jspa?threadID=692710(还有另一个 - 第三个解决方案建议在那里)
    猜你喜欢
    • 2021-08-11
    • 2019-11-24
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多