SQLPlus - spooling to multiple files from PL/SQL blocks

后端 未结 6 1029
说谎
说谎 2020-12-17 04:06

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

6条回答
  •  感情败类
    2020-12-17 05:02

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

    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
    

提交回复
热议问题