简体   繁体   English

在sqlplus中执行时将plsql错误消息重定向到日志文件

[英]redirect plsql error message to a log file when executing it in sqlplus

Need a way to redirect PL/SQL program error message to a log file when executing it in sqlplus. 在sqlplus中执行时,需要一种方法将PL / SQL程序错误消息重定向到日志文件。

Say the PL/SQL program is named send_2012.sql and it has the following exception block 假设PL / SQL程序名为send_2012.sql ,它具有以下异常块

EXCEPTION
        WHEN NO_DATA_FOUND
        THEN
                var_err := 'Data not found. ';
        WHEN OTHERS
        THEN
                var_err := 'Error in '
                        || $$plsql_unit
                        || ' | '
                        || SQLERRM
                        || ' | '
                        || 'Details: '
                        || DBMS_UTILITY.format_error_backtrace;
END;

To run the PL/SQL program in a KornShell (ksh) script, I have: 要在KornShell(ksh)脚本中运行PL / SQL程序,我有:

sqlplus some_username/'some_password' @some_database \
            @/some/directory/send_2012.sql \
            $parameter1 $paramenter2

Suppose error occurs when executing send_2012.sql , how can I redirect the error message from var_err to /some/log/directory/log_send_2012.txt ? 假设执行send_2012.sql时发生错误,如何将错误消息从var_err重定向到/some/log/directory/log_send_2012.txt

Much appreciated. 非常感激。

Setup your script like this: 像这样设置你的脚本:

-- test.sql script run from sqlplus
set serveroutput on
set echo on
WHENEVER SQLERROR EXIT SQL.SQLCODE
spool on
spool test.log

declare
  l_val date;
begin
  select sysdate into l_val from dual where 1=0;
exception
  when others then raise;
end;
/

spool off

log into sqlplus from that directory and run: 从该目录登录sqlplus并运行:

SQL>@test.sql

You'll find the exceptions in the log file (test.log). 您将在日志文件(test.log)中找到异常。

I worked around the logging issue, here is what I did: 我解决了日志记录问题,这就是我所做的:

Within the pl/sql program I inserted DBMS_PUTLINE("error messages goes here, etc"); 在pl / sql程序中我插入了DBMS_PUTLINE("error messages goes here, etc"); to both the program body and exception sections. 程序正文和例外部分。

When calling sqlplus from a Korn shell script, I used a regular output redirect to log pl/sql exceptions: 从Korn shell脚本调用sqlplus时,我使用常规输出重定向来记录pl / sql异常:

sqlplus some_username/'some_password' @some_database \
            @/some/directory/send_2012.sql \
            $parameter1 $paramenter2 \
            > /some/log/directory/send_2012.log

What I did may not be the best solution. 我所做的可能不是最好的解决方案。 Wrap Spool before and after your pl/sql program may give you more options on formatting, but it may also include the output result (say from the select statement) when system executes the program successfully. 在pl / sql程序之前和之后换行Spool可以为您提供更多格式化选项,但是当系统成功执行程序时,它也可能包括输出结果(例如来自select语句)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM