【问题标题】:Unable to geneate csv file from by executing .sql through shell无法通过 shell 执行 .sql 来生成 csv 文件
【发布时间】:2021-12-07 19:43:23
【问题描述】:

我正在尝试通过调用 .sql 文件来生成 csv。问题在于执行时正在创建一个空文件。对数据库运行 sql 会返回记录。抱歉,如果这个问题已经得到解答,但我无法找到任何解决我问题的答案。

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}
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
echo "\nChecking for ORACLE errors..."                            >> ${LOGFILE}
grep 'ORA-' $LOGFILE                                              >> ${LOGFILE}
rc=$?
if [ ${rc} = 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

捕获的日志文件

./100.sh 开始于 2021-10-20:20:14:22 检查 ORACLE 错误... 未发现 ORACLE 错误。

【问题讨论】:

    标签: oracle shell unix


    【解决方案1】:

    csv 文件为空的原因可能是因为您有一个未捕获的错误。 sqlplus 是一个二进制程序,因此它将始终以 0 退出,除非您使用 whenever sqlerrorwhenever 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
    

    【讨论】:

      猜你喜欢
      • 2012-10-15
      • 2010-11-30
      • 2020-04-16
      • 1970-01-01
      • 2018-11-14
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多