SQLPlus - 从 PL/SQL 块假脱机到多个文件
我有一个将大量数据返回到 CSV 文件的查询.太多了,事实上,Excel 无法打开它 - 行太多了.有没有办法控制 spool
在每次处理 65000 行时将其假脱机到一个新文件?理想情况下,我希望在按顺序命名的文件中输出我的输出,例如 large_data_1.csv
、large_data_2.csv
、large_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?
(甲骨文 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
相关文章