csv 文件为空的原因可能是因为您有一个未捕获的错误。 sqlplus 是一个二进制程序,因此它将始终以 0 退出,除非您使用 whenever sqlerror 和 whenever oserror 来捕获执行中产生的异常,无论是由于数据库错误还是操作系统问题。
12c 之前
FILE="CASE.csv"
export dest_loc=/dest/loc
export LOGFILE=$LOG/100.$NOW_TS.log
echo "$0 started at $(date +"%Y-%m-%d:%H:%M:%S")" > ${LOGFILE}
sqlplus -s $DB_LOGIN << EOSQL >> ${LOGFILE}
WHENEVER SQLERROR EXIT FAILURE;
WHENEVER OSERROR EXIT FAILURE;
SET echo OFF
SET feedback OFF
SET sqlprompt ''
SET TERMOUT OFF
SET UNDERLINE OFF
SET trimspool ON
SET trimout ON
SET SQLBLANKLINES ON
SET PAGESIZE 50000
SET COLSEP "|"
SET LINESIZE 650
SPOOL $dest_loc/$FILE
@$SRC/sql_to_run.sql
SPOOL OFF
EOSQL
rc=$?
if [ ${rc} -ne 0 ];
then
echo "\nThere has been an ORACLE error. RC = $RC" >> ${LOGFILE}
echo "\n$0 ended at $(date +"%Y-%m-%d:%H:%M:%S")" >> ${LOGFILE}
echo "####################################" >> ${LOGFILE}
exit 2
else
echo "\nNo ORACLE errors found." >> ${LOGFILE}
fi
12c 或更高
如果您在此版本中,您可能希望使用功能set markup csv 来生成文件。
FILE="CASE.csv"
export dest_loc=/dest/loc
export LOGFILE=$LOG/100.$NOW_TS.log
echo "$0 started at $(date +"%Y-%m-%d:%H:%M:%S")" > ${LOGFILE}
sqlplus -s $DB_LOGIN << EOSQL >> ${LOGFILE}
WHENEVER SQLERROR EXIT FAILURE;
WHENEVER OSERROR EXIT FAILURE;
SET echo OFF
SET feedback OFF
SET SQLBLANKLINES ON
SET PAGESIZE 50000
SET LINESIZE 650
SET TERMOUT OFF
SET MARKUP CSV ON DELIMITER "|"
SPOOL $dest_loc/$FILE
@$SRC/sql_to_run.sql
SPOOL OFF
EOSQL
rc=$?
if [ ${rc} -ne 0 ];
then
echo "\nThere has been an ORACLE error. RC = $RC" >> ${LOGFILE}
echo "\n$0 ended at $(date +"%Y-%m-%d:%H:%M:%S")" >> ${LOGFILE}
echo "##########################################" >> ${LOGFILE}
exit 2;
else
echo "\nNo ORACLE errors found." >> ${LOGFILE}
fi
测试 Sqlplus 退出状态
$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Thu Oct 21 08:32:27 2021
Version 19.6.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0
SQL> create table t1 ( c1 number );
create table t1 ( c1 number )
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0
[ftpcpl@scglvdoracd0006 ~]$ echo $?
0
$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Thu Oct 21 08:32:43 2021
Version 19.6.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0
SQL> whenever sqlerror exit failure;
SQL> create table t1 ( c1 number );
create table t1 ( c1 number )
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0
$ echo $?
1