oracle中怎么设置UTL

57次阅读
没有评论

共计 1744 个字符,预计需要花费 5 分钟才能阅读完成。

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; 

看完上述内容,你们掌握 oracle 中怎么设置 UTL_FILE_DIR 参数的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注丸趣 TV 行业资讯频道,感谢各位的阅读!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-04发表,共计1744字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)