【问题标题】:Oracle SQL Script - Why spool creates file twiceOracle SQL 脚本 - 为什么假脱机创建文件两次
【发布时间】:2020-11-30 02:28:09
【问题描述】:

我使用 Oracle SQL Developer 运行以下 SQL 脚本。我不知道为什么它会创建两个假脱机文件,并且它还会在 Oracle SQL Developer 控制台中显示它不应该的整个操作。可能是因为每个删除语句都有SET FEEDBACK ONSET FEEDBACK OFF

它会创建两个文件,并且只有在有一些记录需要删除时才会在脚本输出中显示所有内容。

脚本的调用

@"C:\RM.sql"

脚本

SET PAGES 0
SET LINESIZE 10000
SET TRIMS ON
SET ECHO OFF
SET HEADING ON
SET VERIFY OFF
SET FEEDBACK OFF
SET TERMOUT OFF
SET SERVEROUTPUT on size 1000000
SET TIMING OFF
SET COLSEP '|'

alter session set NLS_DATE_FORMAT = 'dd.mm.yyyy hh24:mi:ss';

whenever oserror exit -1
whenever sqlerror exit -2

-- The current timestamps into 'times' variable
column times new_value times noprint
select to_char(sysdate, 'YYYYMMDD_HH24MISS') times from DUAL; 

-- variables definition
define output_file="C:\temp\filename_&times..log"

-- echo some text to the standard output
SET TERMOUT ON
PROMPT
PROMPT "The script counts and reports the records to be deleted..."
PROMPT
SET TERMOUT OFF

-- write the results to the 'output_file' file
spool &output_file

PROMPT
PROMPT "BEGIN------------------------------"
PROMPT "delete from the_table1"
select 'Total records BEFORE delete: ' || count (*) as count_records from the_table1;

SET FEEDBACK ON
PROMPT "Actual deletion of records..."
delete from the_table1;
commit;
SET FEEDBACK OFF
PROMPT "END------------------------------"

PROMPT
PROMPT "BEGIN------------------------------"
PROMPT "delete from the_table2"
select 'Total records BEFORE delete: ' || count (*) as count_records from the_table2;

SET FEEDBACK ON
PROMPT "Actual deletion of records..."
delete from the_table2;
commit;
SET FEEDBACK OFF
PROMPT "END------------------------------"

PROMPT
PROMPT "The script completed..."

SPOOL OFF

SET TERMOUT ON
PROMPT
PROMPT "&output_file has been successfully ended."
PROMPT
PROMPT

EXIT
;

【问题讨论】:

  • 我刚刚使用 SQL Developer 版本 20.2 运行了这个场景,并且只有一个日志文件被 SPOOLed/created
  • 这里一样,只有一个文件。顺便说一句,如果你要删除整个表,你不应该截断它吗?
  • @thatjeffsmith 感谢您的反馈。我不知道为什么我现在不能重现它。
  • @RobertoHernandez 感谢您的反馈。我不知道为什么我不能重现它。
  • @Sherzad 您能否在您的问题中提供脚本执行的输出,表明它实际上正在创建两个假脱机文件?

标签: oracle oracle-sqldeveloper sqlplus sql-scripts spool


【解决方案1】:

我似乎记得TERMOUTECHO 选项取决于sqlplus 的运行方式,并且可以给出不同的结果,类似于您所看到的。

我使用sqlplus 作为与运行环境交互如何影响输出的示例。这不是一个真正的答案,我无法重现我以前经常看到的场景,但我希望以下内容可能会提供一些关于可能遇到的情况的提示。

从一个简单的测试脚本开始test_off.sql:

set echo off
set termout off

prompt text1

spool f1.txt
prompt text2
spool off
prompt text 3
quit

现在让我们以多种不同的方式运行它:

1 非交互式参数化

$ sqlplus -S un/pw@idb @test_off.sql


$ cat f1.txt
text2

没有输出到屏幕,创建了假脱机文件,只有假脱机内容。

2。交互式

$ sqlplus -S un/pw/@db
@test_off.sql
$ cat f1.txt
text2

没有输出到屏幕,创建了假脱机文件,只有假脱机内容。

3.作为管道源运行

$ cat test_off.sql | sqlplus -S un/pw@db
text1
text2
text 3

这里我们可以在屏幕上看到所有PROMPT 命令结果。

如果在 HEREDOC 块中运行,可能会看到类似的效果。

我肯定已经看到(在旧版本中)一些奇怪的不一致之处,也许您的 SQL Developer 也有类似的问题。

【讨论】:

  • 感谢您的信息。不幸的是,我无法重现我解决的更多问题。
猜你喜欢
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2020-07-12
相关资源
最近更新 更多