shell - 如何在 shell 脚本中捕获 SQLPlus 退出代码?

标签 shell error-handling scripting sqlplus ksh

我有一个 KornShell (ksh) 脚本,它登录到 SQL*Plus 并执行一个脚本。在 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
/

最佳答案

你有没有试过使用

whenever sqlerror exit sql.sqlcode

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

关于shell - 如何在 shell 脚本中捕获 SQLPlus 退出代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1402610/

相关文章:

ios - 构建我的项目时出现 Xcode 10 beta 错误?

linux - Bash 计算执行时间

python - Scrapy-Splash 错误 400 : "description": "Required argument is missing: url"

linux - 无法读取输出以运行下一个命令

BASH 脚本 : whiptail file select

bash - 为什么 bash 中的 $(( sum += i )) 告诉我 "command not found"?

linux - 使用 sed 提取子表达式

shell - 如何设置vagrant用户对$SHELL使用/bin/bash

iOS:在哪里可以找到 iOS OSStatus 代码的完整列表?

node.js - 如何在 Node/Express 客户端中正确呈现服务器端错误?