0
前段時間,我寫了一個小例程來針對Oracle數據庫運行一些快速n'髒查詢(並指出它不用於大型查詢),但也希望解析錯誤有點容易一些。具體情況如下:Ksh函數用於返回值查詢Oracle
# 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/[email protected]$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;
,它仍然不會產生正確的返回值,因此缺少錯誤測試。
我對sqlplus
和ksh
不是特別好,可能只是缺少一些明顯的東西......有人可以借我一隻手嗎?
謝謝!
這實際上就是我要做的就是在啤酒查詢其他功能...我想保持這種功能雖然簡單,但是,真希望我有辦法,而不解析它讀取查詢的實際結果或使用文件......這可能會讓我頭疼,同時發生多次執行和腳本異常中斷(即解析錯誤文件或生活臨時文件)。正如我所說,它在更大的功能中起作用..只是不想在這個簡單的問題中產生這種麻煩.. – filippo 2012-01-30 17:10:09
我不知道Korn Shell是否可以做你想做的事情。 Bash允許從子shell(命令替換)的退出狀態返回到主shell,如果部分賦值語句,但對於KSH,您最好的選擇可能是臨時文件。你總是可以爲臨時文件創建一個有保證的唯一文件名,並且每天清除它們... – 2012-01-30 19:01:07
我最好不要將userid和密碼傳遞給SQL * Plus - 使用'sqlplus/nolog'選項並且那麼立即使用'CONNECT $ eSQLU/$ eSQLP @ $ eSQLS'就可以避免這種情況。 – 2012-01-30 20:41:58