【问题标题】:How to Spool file if only data exists in oracle?如果 oracle 中只存在数据,如何假脱机文件?
【发布时间】:2020-03-24 02:29:37
【问题描述】:

我正在使用以下脚本生成 csv 文件。如果脚本运行并获得结果,我们将连同列标题一起写入 CSV 文件。如果我们没有得到任何结果,则只有标头被写入 CSV 文件。如果我们没有得到任何结果,我不想将任何东西写入 CSV 文件。 如何在下面的脚本文件中实现它?

脚本

SET FEEDBACK OFF;
SET TERMOUT OFF;
SET SQLFORMAT csv;

spool status.csv;

select 
    batch_id,   
    batch_type,
    bu_code,
    bu_type,
    status,
    error_text,
    cre_user_id,
    to_char(upd_dtime,'DD-MON-YYYY HH.MI.SS AM') as upd_dtime,
    to_char(status_created_dtime,'DD-MON-YYYY HH.MI.SS AM') as status_created_dtime,
    to_char(status_queued_dtime,'DD-MON-YYYY HH.MI.SS AM') as status_queued_dtime,
    to_char(status_running_dtime,'DD-MON-YYYY HH.MI.SS AM') as status_running_dtime     
 from cox_item;

 spool off;

disconnect;

exit;

【问题讨论】:

    标签: sql oracle oracle11g oracle10g sqlplus


    【解决方案1】:

    这里有一个选项:

    • 声明一个变量
    • 检查表中是否存在某些内容并将结果放入该变量中
    • where 子句中使用它(变量)

    像这样:我想假脱机这两个表的内容;一个包含行,另一个不包含:

    SQL> select * from dept;
    
        DEPTNO DNAME                LOC
    ---------- -------------------- --------------------
            10 ACCOUNTING           NEW YORK
            20 RESEARCH             DALLAS
            30 SALES                CHICAGO
            40 OPERATIONS           BOSTON
    
    SQL> select * from cars;
    
    no rows selected
    
    SQL>
    

    你的脚本(我保存为p.sql

    SET FEEDBACK OFF;
    SET TERMOUT OFF;
    SET SQLFORMAT csv;
    
    spool status.txt;
    
    var l_cnt number;
    
    exec select max(1) into :l_cnt from dual where exists (select null from dept);
    
    select *
    from dept
    where :l_cnt = 1;
    
    
    
    exec select max(1) into :l_cnt from dual where exists (select null from cars);
    
    select *
    from cars
    where :l_cnt = 1;
    
    spool off;
    

    执行与结果:

    SQL> @p
    SQL> $type status.txt
    
        DEPTNO DNAME                LOC
    ---------- -------------------- --------------------
            10 ACCOUNTING           NEW YORK
            20 RESEARCH             DALLAS
            30 SALES                CHICAGO
            40 OPERATIONS           BOSTON
    
    SQL>
    

    我觉得不错;第一个表在这里,另一个不在(甚至没有它的标题)。

    【讨论】:

    • 嗨,我的问题是如果表存在并且查询没有给出任何结果,那么我们不应该在 csv 文件中写入任何内容
    • 这是一种适用于很多情况的好技术 - 基本上是在没有通常编程结构的工具中执行 IF 语句。
    【解决方案2】:

    找到记录后,将生成假脱机 sql 和数据文件 set feedback off termout on autoprint on serverout on echo off head off pages 0 newp 0 host del c:\sql\01.txt spool c:\sql\01.sql select ' set head on newp 0 termout on autoprint on serverout on echo off ' ||chr(10) ||'spool c:\sql\01.txt ' || chr(10) || 'select 1,2,3,4,5 from user_tables where rownum<10 ;' || chr(10) || 'spool off ' from dual where exists (select 1,2,3,4,5 from user_tables where rownum<10 ); spool off; @c:\sql\01.sql /

    当没有找到记录时,spool sql 文件将为空,并且不会生成/spooled 数据文件

    set feedback off termout on autoprint on serverout on echo off head off pages 0 newp 0 host del c:\sql\01.txt spool c:\sql\01.sql select ' set head on newp 0 termout on autoprint on serverout on echo off ' ||chr(10) ||'spool c:\sql\01.txt ' || chr(10) || 'select 1,2,3,4,5 from user_tables where rownum<10 ;' || chr(10) || 'spool off ' from dual where exists (select 1,2,3,4,5 from user_tables where rownum<10 and 1=2 ); spool off; @c:\sql\01.sql /

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      • 2015-06-22
      相关资源
      最近更新 更多