redirect plsql error message to a log file when ex

2019-05-10 14:12发布

Need a way to redirect PL/SQL program error message to a log file when executing it in sqlplus.

Say the PL/SQL program is named send_2012.sql and it has the following exception block

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:

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?

Much appreciated.

2条回答
Summer. ? 凉城
2楼-- · 2019-05-10 14:32

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:

SQL>@test.sql

You'll find the exceptions in the log file (test.log).

查看更多
小情绪 Triste *
3楼-- · 2019-05-10 14:40

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"); 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:

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.

查看更多
登录 后发表回答