[英]Error while executing Merge through shell script
I am trying to execute a script which calls Merge statement through sqlplus. 我正在尝试执行一个通过sqlplus调用Merge语句的脚本。
The merge statement is executing fine but when I am executing it through shell script, it's giving an error : merge语句执行得很好,但是当我通过shell脚本执行它时,出现了错误:
current_time=`date`
echo "FSG_PRCB_PRVD_FAC_SEQ table UPDATE STARTED at $current_time"
Result=`sqlplus -s $TgtUsrID/$TgtPswd@$TgtServer <<eof
whenever sqlerror exit sql.sqlcode;
merge into FSG_WRK.FSG_PRCB_FAC_REF_DATA T1
using
(select NUM_ID, ALPHA_NUM_ID from FSG.FSG_PRCB_ADDRESS_CROSSWLK) T2
ON (T1.ADDR_TYPE=T2.ALPHA_NUM_ID)
when matched then
update set T1.ADDR_TYPE_N = T2.NUM_ID ;
eof
`
ERRORCODE=$?
#Check the return code from SQL Plus
if [ $ERRORCODE != 0 ]
then
echo "********************"
echo "ERROR: Updating Status Failed. ErrorCode: $ERRORCODE"
exit -1
else
echo "********************"
echo "UUpdating Status returns $Result"
current_time=`date`
echo "Updating Status Query finished at $current_time"
fi
Output : 输出:
updating status failed.Errorcode:174 更新状态失败。错误代码:174
I am not sure what went wrong. 我不确定出了什么问题。
Using exit sql.sqlcode
can be misleading, or even dangerous. 使用exit sql.sqlcode
可能会引起误解,甚至是危险的。
From The Linux Documentation Project : 来自Linux文档项目 :
Out of range exit values can result in unexpected exit codes. 超出范围的退出值可能会导致意外的退出代码。 An exit value greater than 255 returns an exit code modulo 256. For example, exit 3809 gives an exit code of 225 (3809 % 256 = 225). 大于255的退出值将返回以256为模的退出代码。例如,出口3809给出的退出代码为225(3809%256 = 225)。
You're seeing the exit code reported as 174, but the original error code could have been anything where mod(<actual code>, 256)
evaluates to 174. You'll be able to see the actual error code and message in your $Result
variable. 您看到的退出代码报告为174,但是原始错误代码可能是mod(<actual code>, 256)
计算为174的任何内容。您将能够在$Result
看到实际的错误代码和消息。 $Result
变量。
A likely candidate is an ORA-00942 error (as mod(942, 256)
is 174) - and as you've said the merge works standalone, you'll probably find that the user you're connecting to and running the merge as, represented by $TgtUsrID
, does not have the necessary privileges on the tables owned by FSG
and/or FSG_WRK
. 可能的候选者是ORA-00942错误(因为mod(942, 256)
为174)-正如您所说的合并是独立运行的,您可能会发现您正在连接并以以下身份运行该用户由$TgtUsrID
表示的对FSG
和/或FSG_WRK
拥有的表没有必需的特权。
This shows that the reported exit code can be confusing or misleading. 这表明报告的退出代码可能会造成混淆或误导。 The potentially dangerous part if if you get a real error code which is an exact multiple of 256, such as the plausible ORA-01536 "space quota exceeded for tablespace %s'. As mod(1536, 256)
is zero, $ERRORCODE
will be zero too, so your check: 如果您得到的实际错误代码是256的整数倍,则可能是危险的部分,例如,合理的ORA-01536“表空间%s的空间配额超出了。由于mod(1536, 256)
为零, $ERRORCODE
将也为零,所以您的检查:
if [ $ERRORCODE != 0 ]
will be false even though an error did occur. 即使确实发生错误,也会为假。
It will probably be simpler to change your SQL to have whenever sqlerror exit failure
(maybe with the addition of rollback
?), and then your check for non-zero will work; whenever sqlerror exit failure
更改您的SQL可能会更简单(也许添加了rollback
吗?),然后您可以检查非零值。 and you can use $Result
to see/report what was actually wrong. 您可以使用$Result
查看/报告实际出了什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.