【问题标题】:Show errors in sql plus在 sql plus 中显示错误
【发布时间】:2020-07-30 17:13:18
【问题描述】:

我有一个文件myFile.sql,其中包含要编译的脚本列表:

@"Directory\package1.sql"
@"Directory\package2.sql"
@"Directory\package3.sql"
@"Directory\package4.sql"

我有以下脚本:

SPOOL Directory\Upgrade.log
@"Directory\myFile.sql"
SPOOL OFF

myFile.sql 中的某些包有错误,但在Upgrade.log 我没有错误的详细信息,我有消息警告:Package body created with compilation errors. 如何在 MyFile.sql 的每一行之后添加错误详细信息而不添加 SHOW ERR ? 在 upgrade.log 我想要这个:

Package1 created
Warning Package body created with compilation errors.
**Error detail1**

Package2 created
Warning Package body created with compilation errors.
**Error detail2**

如果出现错误,我需要在 sqlplus 中添加一个钩子,以便在每次创建包后自动显示错误 感谢您的帮助。

【问题讨论】:

    标签: sql oracle stored-procedures plsql sqlplus


    【解决方案1】:

    一种方法是查询字典视图USER_ERRORS,或ALL_ERRORS

    来自the documentation

    ALL_ERRORS 描述当前用户可访问的存储对象的当前错误。

    USER_ERRORS 为当前用户拥有的对象提供相同的信息。

    【讨论】:

    • 我有警告,因为包相互依赖,一旦脚本完成,我使用 EXEC DBMS_UTILITY.COMPILE_SCHEMA(USER,FALSE); 重新编译所有无效对象;我需要的是在 myFile.sql 中的每一行之后显示错误详细信息
    • @BilelChaouadi:您可以过滤列ATTRIBUTE 以将警告与错误分开。
    • 我知道,但我需要在每行之后添加警告,因为在我的日志文件中我编译了包 1 的警告,包 2 然后警告...。我需要在之后添加错误详细信息警告,我想将数据添加到日志文件并创建相关的行包
    • 如果出现错误,我需要一个钩子在每次编译后自动在 sql plus 中显示错误
    • 受访者为您提供了获取错误的方法。但是您仍然说“我需要在每一行之后添加警告”,这不会发生。因此,在糟糕的情况下选择最好的并顺其自然。你想要的不会发生,因为 DIANA(plsql 编译器/解释器)不提供它。
    【解决方案2】:

    从 Oracle 11.1 开始,您可以使用 SQLPlus 错误日志记录 功能。你可以阅读更多关于SQL*Plus error logging – New feature release 11.1的信息。

    SQL*Plus 错误日志默认设置为OFF。因此,您需要set errorlogging on 才能使用SPERRORLOG 表。

    演示:

    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    
    SQL> desc sperrorlog;
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
    
     USERNAME                                           VARCHAR2(256)
     TIMESTAMP                                          TIMESTAMP(6)
     SCRIPT                                             VARCHAR2(1024)
     IDENTIFIER                                         VARCHAR2(256)
     MESSAGE                                            CLOB
     STATEMENT                                          CLOB
    
    SQL> truncate table sperrorlog;
    
    Table truncated.
    
    SQL> set errorlogging on;
    SQL> selct * from dual;
    SP2-0734: unknown command beginning "selct * fr..." - rest of line ignored.
    SQL> select timestamp, username, script, statement, message from sperrorlog;
    
    TIMESTAMP   USERNAME     STATEMENT           MESSAGE
    ----------- --------     ------------------  -------
    17-APR-2020 SCOTT        selct * from dual;  SP2-0734: unknown command beginning "selct * fr..." - rest of line ignored.
    

    同样,您也可以捕获 PLS 错误。它们将以错误代码SP 开头。

    【讨论】:

    • 谢谢@Lalit,但我想自动执行,无需添加:选择时间戳、用户名、脚本、语句、来自 sperrorlog 的消息;因为 myFile.sql 包含 300 多个要编译的脚本。可能是读取文件 myFile.sql,而不是在每一行中循环并自动添加显示错误
    • sperrorlog 由 Oracle 自动创建,您无需添加任何列等。只需设置参数即可。
    • 但是我需要查询 sperrorlog 来获取错误:( 我需要在每个包创建后自动将此错误保存在我的日志文件中
    • @BilelChaouadi 在您的 SQL 文件中,只需添加一行 SELECT * FROM sperrorlog;,这将自动假脱机到您的日志文件中。编译错误不同于运行时错误。所有编译失败的对象都将处于无效状态。
    • 但如果我这样做,我会复制日志。编译所有脚本后,在我的日志文件中,我有包 1 ctrated,警告:packege created with errors,package 2 ctrated,警告:packege created with errors ....我希望将每个错误添加到与每个错误的编译相关联的行脚本
    猜你喜欢
    • 2014-02-05
    • 2020-04-24
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2015-10-12
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多