oracle中设置UTL_FILE_DIR参数


 oracle中设置UTL_FILE_DIR参数
第一步:以管理员用户登陆  
   如:conn    sys/password@sid    as    sysdba

第二步:设置可操作目录  
需要指定utl_file包可以操作的目录。在oracle    10g以前,可以用以下方法:  
   1、alter    system    set    utl_file_dir='e:\utl'    scope=spfile;
   长路径要使用'',如:utl_file_dir='c:\my    temp';    
   2、在init.ora文件中,配置如下: UTL_FILE=E:\utl或者UTL_FILE_DIR=E:\utl  
  在oracle10g中建议用以下方法配置:CREATE    DIRECTORY    utl    AS   'E:\utl';  

第三步:授权给指定用户,以便执行utl_file  
   GRANT    EXECUTE    ON    utl_file    TO    scott;  
   
第四步:conn    scott/tiger  
   就可以正常使用utl_file了。    

第五步:文件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;  
     
   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; 

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

转载于:http://blog.itpub.net/29519108/viewspace-2142124/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值