Oracle的UTL_FILE.FOPEN的用法

來源:互聯網
上載者:User

Oracle提供的檔案操作包UTL_FILE包中的UTL_FILE.FOPEN負責開啟一個檔案。

UTL_FILE.FOPEN(location in varchar2, filename in varchar2, open_mode in varchar2) return FILE_TYPE;
Location 是路徑參數,
FILENAME 是檔案名稱,
OPEN_MODE是開啟模式,'R'是讀文本,'W'是寫文本,'A'是附加文本,參數不分大小寫,如果指定'A'但是檔案不存在,它會用'W'先建立出來,'W'有覆蓋的功能;

其中的location並不能簡單的指定為'D:\temp'等路徑,要建立一個DIRECTORY變數並付給許可權(必須以DBA身份登入):

Sql代碼

  1. create or replace directory D_OUTPUT as 'D:\TEMP';   
  2. grant read,write on directory D_OUTPUT to testdb;   
  3. GRANT EXECUTE ON utl_file TO testdb;  
之後就可以用UTL_FILE包建立檔案了

Sql代碼
  1. V_FILE UTL_FILE.FILE_TYPE;   
  2. V_FILE := UTL_FILE.FOPEN('D_OUTPUT', 'Data.txt', 'w');  
就可以在資料庫伺服器的D:\TEMP建立Data.txt

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.