【问题标题】:sending query to sqlplus向 sqlplus 发送查询
【发布时间】:2013-01-15 13:26:31
【问题描述】:

我有一个脚本文件,它将查询发送到 sqlplus 并将输出发送到其他输出文件。 然而,这个输出文件包含一些来自 sqlplus 的信息——某种问候。

SQL*Plus: Release 11.2.0.3.0 Production on Tue Jan 15 12:30:40 2013                         

Copyright (c) 1982  2011     Oracle.  All rights reserved.                  


Connected to:                           
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production                            
With the Partitioning    Real Application Clusters   Automatic Storage Management    OLAP               
Data Mining and Real Application Testing options                            

有没有办法执行脚本,这样问候语就不会成为文件的一部分?

【问题讨论】:

    标签: sqlplus


    【解决方案1】:

    您可以在连接到数据库时使用sqlplus -s 选项。

    【讨论】:

      【解决方案2】:
      Execsqlstatement1="select
      'ZZZ|'
      ||count(*) total
      from table1"
      
      
      $ORACLE_HOME/bin/sqlplus user/tneal01 << Eossql
          set lines 80
          set pages 50000
          set timing on
          spool /user/tneal01/SPOOL/output.`date +%Y-%m-%d`.tmp
          $Execsqlstatement1
          /
          spool off
          quit;
      Eossql
      

      现在,如果您不需要输出文件中的结果之外的任何内容,您可以在脚本中执行以下操作:

      grep 'ZZZ' $OutPutDir/$OutPutFile.$today.tmp|cut -d"|" -f2- >$OutPutDir/$OutPutFile.$today.txt
      

      下面是一个完整的例子,说明为什么会这样:

      #!/bin/ksh
      
      today=`date "+%Y-%m-%d"`
      OutPutDir="/users/ttead01/SPOOL"
      OutPutFile="output"
      
      #add sql statements here
      
      Execsqlstatement1="select
      'ZZZ|'
      ||count(*) total
      from users"
      
      
      #adding connection details here
      
      $ORACLE_HOME/bin/sqlplus tead01/rangers $SQLPLUS -s / << Eossql
          set lines 80
          set pages 50000
          set timing on
          spool /users/ttead01/SPOOL/output.`date +%Y-%m-%d`.tmp
          $Execsqlstatement1;
          spool off
          quit;
      Eossql
      
      #check to make sure no ORA errors in the query
      
      grep "ORA-" $OutPutDir/$OutPutFile.$today.tmp
      if [ $? -eq 0 ]
      then
          echo "LOG MESSAGE sql select failed"
      
      
       exit 1
      fi
      
      #Seach for ZZZ and cut out junk and send only result to a .txt file
      
      grep 'ZZZ' $OutPutDir/$OutPutFile.$today.tmp|cut -d"|" -f2- >$OutPutDir/$OutPutFile.$today.txt
      
      #Remove the .tmp file if .txt was created
      
      if [ $? -eq 0 ]
      then
        /usr/bin/rm -f $OutPutDir/$OutPutFile.$today.tmp
      else
          exit 1
      fi
      

      现在开始测试:

      bash-3.2$ test.sh

      Copyright (c) 1982, 2010, Oracle.  All rights reserved.
      
      
      
      
      SQL> set autocommit on;
      SQL> rem set linesize 132;
      SQL> define _editor = vi
      SQL> alter session set nls_date_format = 'YYYYMMDD HH24MISS';
      
      Session altered.
      
      SQL> SQL> SQL> SQL> SQL>   2    3    4    5    6    7    8
      TOTAL
      --------------------------------------------
      ZZZ|32
      
      Elapsed: 00:00:00.17
      

      现在让我们看看输出的 .txt 文件:

      bash-3.2$ cat output.2016-05-28.txt
      32
      bash-3.2$
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多