Oracle/SQL 加:如何假脱机日志并在整个脚本中间歇性写入

Oracle/SQL PLUS: How to spool a log and write intermittently throughout script

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

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

是否有任何方法可以实现我们正在尝试做的事情,通过假脱机处理日志文件或其他方法?

这是我目前的代码。

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;

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

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

我想到了两种方法,具体取决于您的 'stuff' 是什么。

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。这确实需要数据库中的一些设置和管理权限来设置允许写入的目录。