SQLPlus-从PL/SQL块假脱机到多个文件 [英] SQLPlus - spooling to multiple files from PL/SQL blocks

查看:102
本文介绍了SQLPlus-从PL/SQL块假脱机到多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,该查询将大量数据返回到CSV文件中.实际上,Excel不能打开太多东西了-行太多了.每当处理65000行时,是否有一种方法可以控制spool假脱机到新文件?理想情况下,我希望将输出保存在按顺序命名的文件中,例如large_data_1.csvlarge_data_2.csvlarge_data_3.csv等...

I have a query that returns a lot of data into a CSV file. So much, in fact, that Excel can't open it - there are too many rows. Is there a way to control spool to spool to a new file everytime 65000 rows have been processed? Ideally, I'd like to have my output in files named in sequence, such as large_data_1.csv, large_data_2.csv, large_data_3.csv, etc...

我可以在PL/SQL块中使用dbms_output来控制输出多少行,但是既然spool似乎无法从PL/SQL块访问,那么我将如何切换文件?

I could use dbms_output in a PL/SQL block to control how many rows are output, but then how would I switch files, as spool does not seem to be accessible from PL/SQL blocks?

(Oracle 10g)

(Oracle 10g)

更新:

我无权访问服务器,因此将文件写入服务器可能无法正常工作.

I don't have access to the server, so writing files to the server would probably not work.

更新2:

某些字段包含自由格式的文本,包括换行符,因此在写入文件后计算换行符并不像在返回数据时对记录进行计数...

Some of the fields contain free-form text, including linebreaks, so counting line breaks AFTER the file is written is not as easy as counting records WHILE the data is being returned...

推荐答案

有解决方案了,不知道为什么我没想到这个...

Got a solution, don't know why I didn't think of this sooner...

基本思想是,主sqplplus脚本会生成一个中间脚本,该脚本会将输出拆分为多个文件.执行中间脚本将执行对rownum施加不同范围的多个查询,并为每个查询假脱机到一个不同的文件.

The basic idea is that the master sqplplus script generates an intermediate script that will split the output to multiple files. Executing the intermediate script will execute multiple queries with different ranges imposed on rownum, and spool to a different file for each query.

set termout off
set serveroutput on
set echo off
set feedback off
variable v_rowCount number;
spool intermediate_file.sql
declare
     i number := 0;
     v_fileNum number := 1;
     v_range_start number := 1;
     v_range_end number := 1;
     k_max_rows constant number := 65536;
begin
    dbms_output.enable(10000);
    select count(*) 
    into :v_err_count
    from ...
    /* You don't need to see the details of the query... */

    while i <= :v_err_count loop

          v_range_start := i+1;
          if v_range_start <= :v_err_count then
            i := i+k_max_rows;
            v_range_end := i;

            dbms_output.put_line('set colsep ,  
set pagesize 0
set trimspool on 
set headsep off
set feedback off
set echo off
set termout off
set linesize 4000
spool large_data_file_'||v_fileNum||'.csv
select data_string
from (select rownum rn, data_object
      from 
      /* Details of query omitted */
     )
where rn >= '||v_range_start||' and rn <= '||v_range_end||';
spool off');
          v_fileNum := v_fileNum +1;
         end if;
    end loop;
end;
/
spool off
prompt     executing intermediate file
@intermediate_file.sql;
set serveroutput off

这篇关于SQLPlus-从PL/SQL块假脱机到多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆