2009-09-09 117 views
8

我有一個登錄到SQL * Plus並執行腳本的KornShell(ksh)腳本。在shell腳本中,我想捕獲執行的SQL語句的狀態碼。目前SQL有一個錯誤,我無法通過檢查$?來捕獲它。 我將如何從sql語句中捕獲成功或錯誤代碼並將其傳遞給shell腳本。如何在shell腳本中捕獲SQLPlus退出代碼?

KSH腳本摘錄:

sqlplus $JDBC_FBUID_U/[email protected]$JDBC_FBDB @${FBC_HOME}/FBCS003.sql ${outputfile} 
if [ $? != 0 ] 
then 
    msg_txt="The execution of Sql script /tmp/FBCS003.sql failed. Please investigate." 
    echo ${msg_txt} 
    echo ${msg_txt} | mailx -r ${fromemail} -s "FBCB003: The execution of Sql script /tmp/FBCS003.sql failed." ${toemail} 
    epage -n ${pagerdef} ${pagernum} "FBCB003: ${msg_txt}" 
    exit 1 
fi 

SQL腳本FBCS003.sql

-- Set SQLPlus variables. 
SET NEWPAGE 0 
SET WRAP OFF 
SET LINESIZE 9999 
SET ECHO OFF 
SET FEEDBACK OFF 
SET VERIFY OFF 
SET HEADING OFF 
SET PAGESIZE 0 
SET COLSEP | 
SET TRIMSPOOL ON 
SET TIMING ON 

-- Open output file 
-- The file path and name are passed from the calling script FBCS003. 
spool &1 

-- Main Select Statement 
select 
ct.fiscal_yr_no, 
ct.acct_per_no, 
ct.bus_unit_id, 
ct.btch_file_seq_no, 
ct.comm_tran_srce_cd, 
ct.rec_no, 
ct.rev_gl_acct_no, 
ct.gl_prod_cd, 
ct.prod_desc, 
ct.paid_ir_no, 
ct.srce_ir_no, 
ct.ir_no_house_acct_rsn_txt, 
ct.vndr_acct_ty_id, 
ct.clnt_na, 
ct.issr_na, 
ct.clnt_na, 
ct.issr_na, 
ct.trd_da, 
ct.setl_da, 
ct.ord_ty_cd, 
ct.actv_ty_cd, 
ct.prin_amt, 
ct.grs_comm_amt, 
ct.net_comm_amt, 
ct.vndr_prod_ty_cd, 
ct.vndr_stmt_id 
from fin.comm_tran ct 
where ct.bus_unit_id = 'EJL' 
and ct.vndr_acct_ty_id in 
('11111111','222222222') 
-- Execute sql statement. 
/

-- Close output file 
spool off 

-- Exit SQL 
exit 
/

回答

12

您是否嘗試過在你的SQL腳本中使用

whenever sqlerror exit sql.sqlcode 

? (見this link

+0

@ChristopheD這正是我所需要的! Thx – AieshaDot 2009-09-10 16:01:49

+3

我可能是錯的,但這不適用於連接錯誤或其他會阻止sqlplus執行sql代碼的錯誤。我現在正在處理這個問題,我找到的最好的解決方案是grep -c「ERROR」$ {LOG_FILE}>/dev/NULL我們將sqlplus輸出傳遞給字符串ERROR而不是檢查日誌文件來自那個的返回碼。有更好的解決方案嗎? – Casey 2010-04-08 15:06:54

+1

該鏈接不再起作用 – 2013-11-28 14:10:05

1

退出從SQL文件與

exit sql.sqlcode; 

捕獲它在$的殼呢?

相關問題