【问题标题】:Error Handling while running sqlplus from shell scripts从 shell 脚本运行 sqlplus 时的错误处理
【发布时间】:2016-03-27 23:25:36
【问题描述】:

我需要验证数据库连接是否成功/失败。

这是我的代码

report=`sqlplus -S /nolog << EOF
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
connect <<username>>/<<Password>>@hostname:port
set linesize 1500
set trimspool on
set verify off
set termout off
set echo off
set feedback off
set heading on
set pagesize 0

spool extract.csv
<<My SQL Query>>
spool off;
exit;
EOF`

我根据线程 Managing error handling while running sqlplus from shell scripts 尝试了以下选项,但它选择了第一个单元格值而不是连接状态。

if [ $report != 0 ]
then
echo "Connection Issue"
echo "Error code $sql_return_code"
exit 0;`enter code here`
fi

请指教。

【问题讨论】:

    标签: unix sqlplus


    【解决方案1】:

    我需要类似的东西,但执行起来有点不同。

    首先,我有 list.txt,其中包含我要测试的数据库。我正在使用钱包连接,但可以对其进行编辑以保存用户名/密码。

    list.txt:

    DB01 INSTANCE1.SCHEMA1 
    DB02 INSTANCE2.SCHEMA2  
    DB03 INSTANCE3.SCHEMA3  
    DB04 INSTANCE4.SCHEMA4  
    

    我有 OK.sql,其中包含我想在每个数据库上运行的查询。

    OK.sql:

    select 'OK' from dual;
    exit
    

    最后,我用test.sh读取list.txt,尝试连接并在每一行运行OK.sql,并将结果记录在(drumroll)result.txt中。

    test.sh:

    . /etc/profile
    rm result.txt
    while read -r name wallet; do
        echo "BEGIN-"$name
        if (sqlplus -S /@$wallet @OK.sql < /dev/null | grep -e 'OK'); then
            echo $name "GOOD" >> result.txt
        else
            echo $name "BAD" >> result.txt
        fi
        echo "END-"$name
    done < list.txt
    

    运行后检查您的 result.txt。

    结果.txt:

    DB01 BAD
    DB02 GOOD
    DB03 GOOD
    DB04 GOOD
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      相关资源
      最近更新 更多