从外壳程序脚本运行sqlplus时管理错误处理 [英] Managing error handling while running sqlplus from shell scripts

查看:100
本文介绍了从外壳程序脚本运行sqlplus时管理错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/bin/sh

echo "Please enter evaluate database username"
read eval_user
echo "Please enter evaluate database password"
read eval_pass
echo "Please enter the database name"
read db_name

LOGFILE=shell_log.txt

$ORACLE_HOME/bin/sqlplus -s /nolog <<-EOF>> ${LOGFILE}
connect $eval_user/$eval_pass@$db_name
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
DBMS_OUTPUT.put_line('Connected to db');
EOF

if [ $? != 0 ]
then 
echo "The upgrade script failed. Please refer to the log results.txt for more information"
echo "Error code $?"
exit 0;
fi

我正在输入垃圾值,试图强制此脚本失败.但是,令人讨厌的是,它一直在前进,而没有提及任何错误代码.在这里还需要做什么?

I am entering garbage values trying to force this script to fail. But, annoyingly, it keeps moving ahead without any mention of any error code. What else needs to be done here?

推荐答案

马克斯说的是正确的.试试这个修改后的脚本

What Max says is correct. Try this modified script

#!/bin/sh

echo "Please enter evaluate database username"
read eval_user
echo "Please enter evaluate database password"
read eval_pass
echo "Please enter the database name"
read db_name

LOGFILE=shell_log.txt

sqlplus -s /nolog <<-EOF>> ${LOGFILE}
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
connect $eval_user/$eval_pass@$db_name
DBMS_OUTPUT.put_line('Connected to db');
EOF

sql_return_code=$?

if [ $sql_return_code != 0 ]
then
echo "The upgrade script failed. Please refer to the log results.txt for more information"
echo "Error code $sql_return_code"
exit 0;
fi

请注意使用sql_return_code捕获SQLPLUS返回码.

Please note the use of sql_return_code to capture the SQLPLUS return code.

DBMS_OUTPUT语句应失败,并显示错误-"SP2-0734:未知命令开始...".您可以在日志文件中找到错误消息.

The DBMS_OUTPUT statement should fail with error - "SP2-0734: unknown command beginning...". You can find the error message in log file.

可以使用错误记录工具在SQLPLUS 11g中捕获sp2错误.请查看 http://tkyte.blogspot. co.uk/2010/04/new-thing-about-sqlplus.html 了解更多信息.

It is possible to trap the sp2 errors in SQLPLUS 11g using the error logging facility. Please have a look at http://tkyte.blogspot.co.uk/2010/04/new-thing-about-sqlplus.html for more information.

这篇关于从外壳程序脚本运行sqlplus时管理错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆