读书人

ORACLE写TXT资料操作

发布时间: 2012-08-07 14:54:48 作者: rapoo

ORACLE写TXT文件操作

1、用管理员账号创建目录并给生成用户授权

-- Create directory create or replace directory DIR_BOOK  as 'D:\DIR_BOOK';

?

grant read,write on directory dir_book to scott;

?

2、编写过程

create or replace procedure p_exp as  cursor v_my_cursor is    select t.id || '|#|' || t.name || '|#|' || t.auther || '|#' || t.version from book t order by t.id;  v_line      varchar2(2000);  v_curr_date varchar2(14);  v_file_name varchar2(37);  v_file_dir  varchar2(10);  v_out_file  utl_file.file_type;begin  v_curr_date := to_char(sysdate, 'yyyymmddhh24miss');  v_file_name := 'book' || v_curr_date || 'A01.txt';  v_file_dir  := 'DIR_BOOK';  v_out_file  := utl_file.fopen(v_file_dir, v_file_name, 'w');  open v_my_cursor;  loop    fetch v_my_cursor      into v_line;    exit when v_my_cursor%notfound;    --开始往文件写内容    utl_file.put_line(v_out_file, v_line);  end loop;  --关闭文件  utl_file.fclose(v_out_file);end;

?

要求:

导出的文件类型为txt文件,采用utf-8编码格式。?

?

说明:

1、需要导出的表是个测试表,数据100W条。第一次导出时耗时1分钟多一点儿,第二次导出时耗时50多秒(大概存过使用之后数据库有缓存吧)。导出之后的txt文件大小为133M左右。

???? 一个小插曲:在windows系统上导出的txt文件,用记事本打开后中文正常显示;换个editplus编辑器打开,它找不到对应的编码,随便指定一个在打开之后,中文显示乱码。解决办法:先用记事本打开,在将文件另存为时选择以utf-8格式即可。

2、之所以采用数据库导出的方式,原因是之前采用JAVA代码写的导数据方式,100W行的数据每读取一条往本地文件里写一条,写完整个文件时,耗时42分钟左右,这斯也忒慢了。

读书人网 >其他数据库

热点推荐