【问题标题】:Using Spool in Oracle 11g在 Oracle 11g 中使用假脱机
【发布时间】:2014-08-19 00:10:39
【问题描述】:

我想在 Oracle 11g 中使用假脱机功能。

我希望将整个输出假脱机到一个文件中,并将输出的子集假脱机到一个单独的文件中。

在下面的示例中,我希望 temp_1.txt 包含来自 A、B、C、D 和 E 的数据

temp_2.txt 我只想要 D 的数据。

sqlplus user/pass@inst

spool on temp_1.txt    
select * from A;
select * from B;
select * from C; 
spool on temp_2.txt
select * from D;
spool off temp_2.txt
select * from E;

exit;

注意:-由于这是非常旧的遗留代码,我无法为 D 编写单独的 sqlplus 会话或重新排序选择。

【问题讨论】:

    标签: oracle oracle11g spool


    【解决方案1】:

    如何在 sqlplus 脚本中完成所有操作。如果您曾经在不同的系统(即 Microsoft Windows)上运行,则需要更改主机命令。但是,它们也需要在 shell 脚本中进行更改。

    spool all_queries.txt
    select * from A;
    select * from B;
    select * from C;
    spool off
    
    spool only_d_query.txt
    select * from D;
    spool off
    
    host cat only_d_query.txt >>all_queries.txt
    
    spool all_queries.txt append
    select * from E;
    spool off
    

    【讨论】:

      【解决方案2】:

      你不能那样做。 The SPOOL command 一次只允许打开一个文件;您的第二个命令,即spool temp_2.txt(没有on)将在打开并开始写入第二个文件之前关闭第一个文件。而off 不接受任何其他参数。

      Usage: SPOOL { <file> | OFF | OUT }
      where <file> is file_name[.ext] [CRE[ATE]|REP[LACE]|APP[END]]
      

      一种解决方案是将语句的输出假脱机到不同的文件:

      spool temp_1.txt
      select * from A;
      select * from B;
      select * from C;
      spool temp_2.txt
      select * from D;
      spool temp_3.txt
      select * from E;
      spool off
      

      ...然后将操作系统中的所有三个文件合并为一个,以获取您的“主”输出文件,同时仍单独保留仅 D 文件。例如:

      cat temp_2.txt >> temp_1.txt
      cat temp_3.txt >> temp_1.txt
      rm temp_3.txt`
      

      如果我理解正确的话,temp_1.txttemp_2.txt 会留下你想要的内容。当然,如果您在 Windows 上使用不同的方法。

      或者,您可以在 PL/SQL 块中运行查询并使用UTL_FILE 将结果写入两个打开文件中的一个或两个。但这需要更多的工作,并且会将文件写入服务器 - 因此您需要对 DIRECTORY 对象的写入权限,并访问指向的底层文件系统目录以检索文件。

      【讨论】:

        猜你喜欢
        • 2011-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        • 2017-06-04
        • 1970-01-01
        • 2011-01-07
        相关资源
        最近更新 更多