oracle 生成txt ,csv ,xls

CREATE or replace DIRECTORY READ_FILE_DIR AS 'D:\abc\'; -- 要手动创建abc目录
select * from dba_directories;
grant read,write on directory READ_FILE_DIR to PUBLIC; --当前用户给其他用户赋权限


create or replace procedure lfz_utl_file_pro as
l_file utl_file.file_type;
cursor c_lifztest_analyse3 is select * from lifztest_analyse3;
BEGIN
  l_file := utl_file.fopen('READ_FILE_DIR', 'aaa.TXT', 'W');--READ_FILE_DIR要大写 'aaa.TXT' 自动创建

  for r_c_lifztest_analyse3 in c_lifztest_analyse3 loop
      utl_file.put_line(l_file,r_c_lifztest_analyse3.hphm1);
  end loop;

  utl_file.fclose(l_file);
END;


r -- read text
w -- write text
a -- append text
rb -- read byte mode
wb -- write byte mode
ab -- append byte mode


第一步:以管理员用户登陆  
   如:conn    sys/password@sid    as    sysdba  
   第二步:设置可操作目录  
   需要指定utl_file包可以操作的目录。在oracle    10g以前,可以用以下方法:  
   1、alter    system    set    utl_file_dir='e:/utl'    scope=spfile;  
   2、在init.ora文件中,配置如下:  
   UTL_FILE=E:/utl或者UTL_FILE_DIR=E:/utl  
   在oracle    10g中建议用以下方法配置:CREATE    DIRECTORY    utl    AS    'E:/utl';  
   参见oracle    online:  
   In    the    past,    accessible    directories    for    the    UTL_FILE    functions    were    specified    in    the    initialization    file    using    the    UTL_FILE_DIR    parameter.    However,    UTL_FILE_DIR    access    is    not    recommended.    It    is    recommended    that    you    use    the    CREATE    DIRECTORY    feature,    which    replaces    UTL_FILE_DIR.    Directory    objects    offer    more    flexibility    and    granular    control    to    the    UTL_FILE    application    administrator,    can    be    maintained    dynamically    (that    is,    without    shutting    down    the    database),    and    are    consistent    with    other    Oracle    tools.    CREATE    DIRECTORY    privilege    is    granted    only    to    SYS    and    SYSTEM    by    default.  
   
   第三步:授权给指定用户,以便执行utl_file  
   GRANT    EXECUTE    ON    utl_file    TO    scott;  
   
   第四步:conn    scott/tiger  
   就可以正常使用utl_file了。    
   
   
   
   
   摘要:本文主要讨论如何利用Oracle的UTL_FILE包来实现对磁盘文件的I/O操作。  
   
     文件I/O对于数据库的开发来说显得很重要,比如如果数据库中的一部分数据来自于磁盘文件,  
           那么就需要使用I/O接口把数据导入到数据库中来。在    PL/SQL中没有直接的I/O接口,  
           一般在调试程序时可以使用Oracle自带的DBMS_OUTPUT包的put_line函数(即向屏幕进行I/O    操作)即可,  
         但是对于磁盘文件的I/O操作它就无能为力了。其实Oracle同样也提供了可以进行文件I/O的实用包-----UTL_FILE包,  
         利用这个实用包提供的函数来实现对磁盘的I/O操作。  
   
     1.    准备工作  
   
     由于Oracle数据库对包创建的目录有一个安全管理的问题,所以并不是所有的文件目录能够被UTL_FILE包所访问,  
           要更新这种目录设置,就得到init.ora里将UTL_FILE_DIR域设置为*,这样UTL_FILE包就可以对所有的目录文件进行访问了。  
   
     2.    文件I/O的实施  
   
     UTL_FILE包提供了很多实用的函数来进行I/O操作,主要有以下几个函数:  
   
     fopen  
   
     打开指定的目录路径的文件。  
   
     get_line  
   
     获取指定文件的一行的文本。  
   
     put_line  
   
     向指定的文件写入一行文本。  
   
     fclose  
   
     关闭指定的文件。  
   
     下面利用这些函数,实现从文件取数据,然后将数据写入到相应的数据库中。  
   
   create    or    replace    procedure    loadfiledata(p_path    varchar2,p_filename    varchar2)    as  
   v_filehandle    utl_file.file_type;    --定义一个文件句柄  
   v_text    varchar2(100);    --存放文本  
   v_name    test_loadfile.name%type;  
   v_addr_jd    test_loadfile.addr_jd%type;  
   v_region    test_loadfile.region%type;  
   v_firstlocation    number;  
   v_secondlocation    number;  
   v_totalinserted    number;  
   begin  
   if    (p_path    is    null    or    p_filename    is    null)    then  
   goto    to_end;  
   end    if;  
   v_totalinserted:=0;  
   /*open    specified    file*/  
   v_filehandle:=utl_file.fopen(p_path,p_filename,'r');  
   loop  
   begin  
   utl_file.get_line(v_filehandle,v_text);  
   exception  
   when    no_data_found    then  
   exit;  
   end    ;  
   v_firstlocation:=instr(v_text,',',1,1);  
   v_secondlocation:=instr(v_text,',',1,2);  
   v_name:=substr(v_text,1,v_firstlocation-1);  
   v_addr_jd:=substr(v_text,v_firstlocation+1,v_secondlocation-v_firstlocation-1);  
   v_region:=substr(v_text,v_secondlocation+1);  
   /*插入数据库操作*/  
   insert    into    test_loadfile  
   values    (v_name,v_addr_jd,v_region);  
   commit;  
   end    loop;  
   <<to_end>>  
   null;  
   end    loadfiledata;  
   
   
   
   可以不用在init.ora中改的  
   只要用管理员的权限登陆,执行:  
   create    directory    UTL_FILE_TEST    as    '/*'    
   应该就可以了,不用新启动oracle的  
   不过其他用户要使用此目录要授权的  
   /  
   
   
   grant    create    any    directory    to    scott;  
     grant    create    any    library    to    scott;  
     create    or    replace    directory    utllobdir    as    'C:/ep';  
   
   
   在initsid.ora文件中,加入或修改  
       设置utl_file_dir的要点:  
     1。    utl_file_dir=*      这表示你能操作任何目录,尽量不要用  
     2。    utl_file_dir=d:/    这表示你能操作d:/目录下的文件,但你不能操作d:/目录下的子目录  
     3。注意在设置  
       utl_file_dir=路径时,如果路径是长路径名,例如c:/my    temp目录,则你必须加上'',例如:  
       utl_file_dir='c:/my    temp'  
     4。utl_file_dir可以是多个路径  
           utl_file_dir=c:/,d:/,d:/temp,'c:/my    temp'  
     5。设置完必须重新启动数据库


from  http://blog.csdn.net/oracle_microsoft/article/details/2030204


(2)spool  在命令行中执行
set trimspool on
set linesize 550
set pagesize 2000
set newpage 1
set heading off
set term off
spool sp_test.txt
      select xh||'|'||hphm1||'|'||hpzl1||'|'||ccdjrq1 from lifztest_analyse3;
spool off


(3) 生成csv xls

C:\Documents and Settings\Administrator>sqlplus js_fjfl/js_fjfl@fjflzx
SQL> set linesize 200
SQL> set term off verify off feedback off pagesize 999
SQL> set markup html on entmap ON spool on preformat off
SQL&gt; spool d:\tables.xls
<br>
SQL&gt; @d:\fjflzxtofjflww\bai.sql
<br>
spool off


猜你喜欢

转载自ujs-lifazhu.iteye.com/blog/1959972