动态导出为csv文件
来源:互联网 发布:网络网名吸引女孩子 编辑:程序博客网 时间:2024/04/28 02:16
在asktom上看到的例子。稍微修改了一下导出文件的格式:
1.输出列名
2.所有列值都以双引号引起来
3.列值中原有的双引号则改成两个双引号
4.去掉前后空格
create or replace procedure dump_to_csv(
p_query in varchar2, --sql query statement
p_dir in varchar2, --the directory of file
p_filename in varchar2, --the export filename
p_max_linesize in number default 32000 --max linesize,must less than 32787
)
is
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
begin
--open file
l_output := utl_file.fopen( p_dir, p_filename, 'w', p_max_linesize);
--define date format
execute immediate 'alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss''';
--open cursor
dbms_sql.parse( l_theCursor, p_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );
--dump table column name
for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;
utl_file.new_line( l_output );
--execute the query statement
l_status := dbms_sql.execute(l_theCursor);
--dump table column value
while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
l_separator := '';
for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || '"' ||
trim(both ' ' from replace(l_columnValue,'"','""')) || '"');
l_separator := ',';
end loop;
utl_file.new_line( l_output );
end loop;
--close cursor
dbms_sql.close_cursor(l_theCursor);
--close file
utl_file.fclose( l_output );
exception
when others then
raise;
end;
/
注意:
p_dir参数要求传入一个directory,可以使用以下语句创建
create directory exp_dir as 'd:';
然后传入参数p_dir=>'EXP_DIR',必须全部采用大写字母。
另外,也可以设置初始化参数utl_file_dir,然后传utl_file_dir的值即可。但是该参数不是动态参数,修改后需要重启数据库才能生效。
限制:
每行最大不能超过32787字节
测试:
以all_objects的内容生成一个160w行数据的表,导出时间约10分钟,导出后的文件约250M,机器:PC (P4 3.0G cpu + 1G mem),同时还开着oracle9201,sql server2000,以及其他一些应用程序。
(需要引用, 请注明出处: http://ningoo.itpub.net)
- 动态导出为csv文件
- 动态导出为csv文件 (2)不带引号!
- 批量导出为csv文件
- PHP 动态导出生成 excel,csv文件
- 导出为CSV文件的类
- git日志导出为csv、excel文件
- VBS脚本 自动更新excel文件 并导出为csv文件
- oracle数据库的表数据导出为csv文件
- 将datatable导出为Excel或CSV文件
- 将数据导出为csv文件(需要javacsv.jar)
- 导出为csv文件时的各种中文问题汇总
- 将任意表数据导出为CSV文件的过程
- 订单等数据导出为Csv文件注意事项
- MongoDB中的数据导出为excel CSV 文件
- Bcp导出数据库数据为CSV文件(不带表头)
- 导出数据库中表数据为CSV文件(带表头)
- 远程从Mongodb 数据库中导出为CSV文件
- 利用Shell将MySQL数据表导出为csv文件
- 正在学习XMPP 转载
- IBM 批发
- 力戒浮躁与功利
- 解决JSP中文乱码问题
- 最后更新的我自己要得到的sql,累了我四天终于弄出了点成绩,虽然很小
- 动态导出为csv文件
- __stdcall 和 __cdecl 的区别
- Struts 处理表单跨页
- 项目记录
- 动态导出为csv文件 (2)不带引号!
- linux 学习笔记(4.26)
- 一道笔试题
- DVD-Video 解谜 - IFO文件 - VTS specific tables
- Windows多线程多任务设计初步