oracle中設定UTL_FILE_DIR引數

studywell發表於2017-07-16

 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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章