简体   繁体   English

Oracle SQL 脚本 - 为什么假脱机创建文件两次

[英]Oracle SQL Script - Why spool creates file twice

I run the following SQL-script using Oracle SQL Developer.我使用 Oracle SQL Developer 运行以下 SQL 脚本。 I don't know why it creates two spool file and it also displays the entire operations in the Oracle SQL Developer console which it should not.我不知道为什么它会创建两个假脱机文件,并且它还会在 Oracle SQL 开发人员控制台中显示它不应该的整个操作。 Maybe it is due to the SET FEEDBACK ON and SET FEEDBACK OFF for each delete statement.可能是由于每个删除语句的SET FEEDBACK ONSET FEEDBACK OFF

It creates two files and displays everything in script-output only when there are some records for deletion.它仅在有一些记录要删除时创建两个文件并在脚本输出中显示所有内容。

The script's call脚本的调用

@"C:\RM.sql"

The script剧本

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
;

I seem to recall TERMOUT and ECHO options depend on how sqlplus is being run and can give different results, similar to what you are seeing.我似乎记得TERMOUTECHO选项取决于sqlplus的运行方式,并且可以给出不同的结果,类似于您所看到的。

I'm using sqlplus as a example of how interactions with the run environment can affect output.我使用sqlplus作为与运行环境交互如何影响 output 的示例。 This isn't really an answer, and I cannot reproduce the scenarios I used to see often, but I hope the following perhaps will offer some pointers as to what might be encountered.这不是一个真正的答案,我无法重现我以前经常看到的场景,但我希望以下内容可能会提供一些关于可能遇到的情况的指示。

Start with a simple test script test_off.sql :从一个简单的测试脚本test_off.sql开始:

set echo off
set termout off

prompt text1

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

Now let's run it a number of different ways:现在让我们以多种不同的方式运行它:

1 Non-interactively Parameterised 1 非交互式参数化

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


$ cat f1.txt
text2

No output to screen, spool file created, with only the spooled contents.没有 output 到屏幕,创建假脱机文件,只有假脱机内容。

2. Interactively 2. 交互式

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

No output to screen, spool file created, with only the spooled contents.没有 output 到屏幕,创建假脱机文件,只有假脱机内容。

3. Run as piped feed 3. 作为管道饲料运行

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

Here we see all the PROMPT command results on screen.在这里,我们可以在屏幕上看到所有的PROMPT命令结果。

Similar effects may be seen if run within a HEREDOC block.如果在 HEREDOC 块中运行,可能会看到类似的效果。

I've definitely seen (in older versions) some strange inconsistencies and perhaps your SQL Developer is similarly afflicted.我肯定已经看到(在旧版本中)一些奇怪的不一致之处,也许您的 SQL 开发人员也受到同样的困扰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM