首页 > 解决方案 > 当使用“WHENEVER SQLERROR EXIT 1”时,为什么 Sqlplus bash 脚本不返回 ORA-02291 完整性约束的错误?

问题描述

我编写了一个 bash 脚本来连接到 sqlplus 并执行特定文件夹中的所有 sql 脚本。下面是脚本的执行方法。

execute() {
if [ ! -d "$DIR_SqlFiles" ]
then
echo "No sql files were found. Cannot find path ${DIR_SqlFiles}"
return 1
fi
echo "`date` :Connecting To ${userName}/******@${serviceName}";
for file in `ls ${DIR_SqlFiles}/*` ; do
echo "`date` :Executing file $file..."
echo "`date` :SQL OUTPUT:";
sqlplus -s ${userName}/${password}@${host}:${port}/${serviceName} <<EOF
WHENEVER OSERROR EXIT 1 ROLLBACK;
WHENEVER SQLERROR EXIT 1 ROLLBACK
@${file};
commit;
quit;
EOF
sql_return_code=$?
if [ ${sql_return_code} != 0 ]
then
echo "`date` ${file} failed"
echo "Error code ${sql_return_code}"
return 1;
fi
done
echo "`date` :completed running all sql scripts in the ${DIR_SqlFiles} folder."
return 0;
}

问题是当 .sql 文件之一具有 ORA-02291:完整性约束时,此脚本不会失败。它返回 0,它只打印“0 行更新”而不打印实际错误。

在其他情况下,它工作正常。例如,如果表名错误(ORA-00942:表或视图不存在),脚本将失败并返回 1。

谁能指出我正确的方向。为什么对于第一种情况错误它没有失败?

标签: bashsqlplusheredoc

解决方案


推荐阅读