ITPub博客

首页 > 数据库 > Oracle > Oracle 9i,10g,11g 下的 UTL_FILE_DIR 及 DIRECTORY - 2

Oracle 9i,10g,11g 下的 UTL_FILE_DIR 及 DIRECTORY - 2

原创 Oracle 作者:tolywang 时间:2014-11-29 17:34:58 0 删除 编辑

directory及utl_file的例子:

 create or replace directory  UTF_DIR_01  as '/u01/test';
 grant read,write on directory  UTF_DIR_01  to wang ;


eg 1: 将某些信息写入文件:

 declare
     v_file utl_file.file_type;
     begin
       v_file := utl_file.fopen('UTF_DIR_01', 'test_dir.txt', 'w');
       utl_file.put_line(v_file , '1. write test msg to utl_file');
       utl_file.put_line(v_file , '2. write test msg to utl_file');
       utl_file.fclose(v_file);
     end;
     /

  fopen      打开指定的目录路径的文件。
 get_line   获取指定文件的一行的文本。
 put_line   向指定的文件写入一行文本。  
  fclose     关闭指定的文件。
 


eg 2: 读取文件所有信息,写入表中。

declare
     v1  VARCHAR2(32767);
     v_file utl_file.file_type;
     begin
       v_file := utl_file.fopen('UTF_DIR_01', 'test_dir.txt', 'r');
       loop
           utl_file.get_line(v_file , v1);
           insert into wang.test_dir values(v1);
           commit;
           -- dbms_output.put_line('v1: ' || v1);
       end loop ;
           utl_file.fclose(v_file);
       exception
         when NO_DATA_FOUND then
           utl_file.fclose(v_file);
     end;
     / 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/35489/viewspace-1351242/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
Oracle , MySQL, SAP IQ, SAP HANA, PostgreSQL, Tableau 技术讨论,希望在这里一起分享知识,讨论技术,畅谈人生 。

注册时间:2007-12-10

  • 博文量
    5595
  • 访问量
    14695370