【问题标题】:How do I capture a SQLPlus exit code within a shell script?如何在 shell 脚本中捕获 SQLPlus 退出代码?
【发布时间】:2010-11-27 00:28:39
【问题描述】:

我有一个登录 SQL*Plus 并执行脚本的 KornShell (ksh) 脚本。在 shell 脚本中,我想捕获已执行的 SQL 语句的状态代码。目前 SQL 存在错误,我无法通过检查 $? 来捕获它。 如何从 sql 语句中捕获成功或错误代码并将其传递给 shell 脚本。

ksh 脚本片段:

sqlplus $JDBC_FBUID_U/$JDBC_FBPWD_U@$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
/

【问题讨论】:

    标签: shell error-handling scripting sqlplus ksh


    【解决方案1】:

    您是否尝试过使用

    whenever sqlerror exit sql.sqlcode
    

    在您的 sql 脚本中? (另见this link

    【讨论】:

    • 我可能是错的,但这不适用于连接错误或其他会阻止 sqlplus 执行 sql 代码的错误。我现在正在工作,我发现的最佳解决方案是 grep -c "ERROR" ${LOG_FILE} >/dev/NULL 我们将 sqlplus 输出传递到字符串 ERROR 的日志文件,然后检查的返回码。有更好的解决方案吗?
    • 连接错误似乎导致 sqlplus 以错误级别 1 退出
    【解决方案2】:

    退出sql文件

    exit sql.sqlcode;
    

    使用 $? 将其捕获到 shell 中?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-28
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 2020-08-12
      • 2013-01-26
      • 2014-10-21
      相关资源
      最近更新 更多