【问题标题】:Oracle/SQL PLUS: How to spool a log and write intermittently throughout scriptOracle/SQL PLUS:如何假脱机日志并在整个脚本中间歇性写入
【发布时间】:2016-01-12 06:49:27
【问题描述】:

弄清楚如何假脱机到文件已经很容易了。我希望在写入每个命令后有一个选项可以写入文本文件。我不确定如何将长脚本的状态传达给团队中的其他人。我们想要的解决方案是将日志文件写入网络驱动器,因为脚本执行时他们将能够跟进。

但是,这似乎只是在 spool 关闭后才将输出写入文件;文件末尾的命令。

有什么方法可以实现我们正在尝试做的事情,无论是通过假脱机日志文件还是其他方法?

这是我到目前为止的代码。

set timing on;
set echo on;

column date_column new_value today_var
select to_char(current_timestamp, 'yyyymmdd_HH24_MI') as date_column
  from dual
/
select current_timestamp from dual;

SPOOL 'Z:\log\KPI\secondary_reporting_&today_var..log'

... lots of stuff...
spool off;

【问题讨论】:

  • 您可能希望在 SPOOL APPEND 之后放置中间 SPOOL OFF,其中 是该命令所有实例的同一个文件。
  • >> 我不知道如何向团队中的其他人传达长脚本的状态

标签: oracle sqlplus


【解决方案1】:

据我所知,无法控制假脱机输出何时写入文件。但是,解决此问题的一种方法可能是完全放弃假脱机并仅重定向输出:

$ sqlplus @/path/to/script.sql >& /path/to/script.log

【讨论】:

    【解决方案2】:

    两种方法浮现在脑海中,这取决于你的“东西”是什么。

    1) 如果您的代码有很多 SQL 语句和 PL/SQL 块,那么您可以反复假脱机一段时间。为此使用spool <filename> append 语句。

    SQL> help spool
    
     SPOOL
     -----
    
     Stores query results in a file, or optionally sends the file to a printer.
     In iSQL*Plus, use the Preferences screen to direct output to a file.
    
     SPO[OL] [file_name[.ext] [CRE[ATE] | REP[LACE] | APP[END]] | OFF | OUT]
    
     Not available in iSQL*Plus
    

    2) 如果您有长时间运行的 PL/SQL 过程,请使用 UTL_FILE 包。请参阅https://docs.oracle.com/html/B14258_02/u_file.htm 了解更多信息。这确实需要数据库中的一些设置和管理权限来设置允许写入的目录。

    【讨论】:

      猜你喜欢
      • 2011-02-03
      • 1970-01-01
      • 2012-01-25
      • 2020-07-02
      • 1970-01-01
      • 2019-10-25
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多