【发布时间】:2012-02-22 09:36:35
【问题描述】:
前段时间,我写了一个小例程来针对 Oracle DB 运行一些快速的 n' 脏查询(我的意思是它不用于大型查询),但也想更容易解析错误.关注:
# Executes the query
#
# Will execute a query contained in the variable named
# in the parameter $4 and store the result in the variable
# named in $5.
# In case of errors (even SQL related) the function should
# exit with status 1, making it possible to "if execQuery".
#
# @param $1 = User
# $2 = Pasword
# $3 = Tns Alias
# $4 = Name of the variable containing the query
# $5 = Name of the variable to hold the result
#
# @return query execution status
function execQuery {
typeset eSQLU=$1
typeset eSQLP=$2
typeset eSQLS=$3
typeset etQUERY=$4
eval typeset eQUERY=\$$etQUERY
typeset eQRES=$5
logMessageFile "DEBUG" "Query: $eQUERY"
typeset res=$(sqlplus -s $eSQLU/$eSQLP@$eSQLS <<EOF
set echo off newpage 0 space 0 pagesize 0 feed off head off verify off lines 999
WHENEVER SQLERROR EXIT 1
$eQUERY
exit;
EOF
)
[[ $? -gt 0 ]] && return 1 || eval "$eQRES=\"$res\""
}
这个函数的想法是,以后我可以做这样的事情:
query="select sysdate from dual;"
if execQuery $RAID_APP_PI_USR $RAID_APP_PI_PWD $RAID_APP_PI_SID query result ; then
echo $result
logMessageFile "INFO" "Inserts into XX successful."
else
logMessageFile "ERROR" "Error insertando XXX."
fi
它有点工作......一个正确编写的查询会做得很好,并且结果变量全部正确评估。问题是错误。如果该示例中的查询类似于select * potato potato;,它仍然不会产生正确的返回值,从而错过了错误测试。
我对@987654324@ 和ksh 不是特别擅长,可能只是漏掉了一些明显的东西……有人可以帮我看看吗?
谢谢!
【问题讨论】: