Oracle 讀寫檔案 bfilename [執行個體]

來源:互聯網
上載者:User
Oracle 讀寫檔案 bfilename [執行個體]
Create directory讓我們可以在Oracle資料庫中靈活的對檔案進行讀寫操作,極大的提高了Oracle的易用性和可擴充性。
其文法為:
CREATE [OR REPLACE] DIRECTORY directory AS 'pathname';

本案例具體建立如下:
create or replace directory exp_dir as '/tmp';
 

目錄建立以後,就可以把讀寫權限授予特定使用者,具體文法如下:
GRANT READ[,WRITE] ON DIRECTORY directory TO username;

例如:
grant read, write on directory exp_dir to eygle;
 

此時使用者eygle就擁有了對該目錄的讀寫權限。

讓我們看一個簡單的測試:
SQL> create or replace directory UTL_FILE_DIR as '/opt/oracle/utl_file';Directory created.
SQL> declare  
 2    fhandle utl_file.file_type;
 3  begin  
 4    fhandle := utl_file.fopen('UTL_FILE_DIR', 'example.txt', 'w');  
 5    utl_file.put_line(fhandle , 'eygle test write one');  
 6    utl_file.put_line(fhandle , 'eygle test write two');  
 7    utl_file.fclose(fhandle);  
 8  end;  
 9  /
 PL/SQL procedure successfully completed.
 SQL> !
 [oracle@jumper 9.2.0]$ more /opt/oracle/utl_file/example.txt eygle test write oneeygle test write two[oracle@jumper 9.2.0]$
 

類似的我們可以通過utl_file來讀取檔案:
SQL> declare  
 2    fhandle   utl_file.file_type;  
 3    fp_buffer varchar2(4000);  
 4  begin  
 5    fhandle := utl_file.fopen ('UTL_FILE_DIR','example.txt', 'R');  
 6    
 7    utl_file.get_line (fhandle , fp_buffer );  
 8    dbms_output.put_line(fp_buffer );  
 9    utl_file.get_line (fhandle , fp_buffer );
 10    dbms_output.put_line(fp_buffer );
 11    utl_file.fclose(fhandle);
 12  end;
 13  /
 eygle test write one
 eygle test write two
 PL/SQL procedure successfully completed.
 

可以查詢dba_directories查看所有directory.
SQL> select * from dba_directories;
      OWNER                          DIRECTORY_NAME                 DIRECTORY_PATH
      ------------------------------ ------------------------------ ------------------------------
      SYS                            UTL_FILE_DIR                   /opt/oracle/utl_fileSYS                            
                                      BDUMP_DIR                      /opt/oracle/admin/conner/bdumpSYS                                                         EXP_DIR                        /opt/oracle/utl_file
 

可以使用drop directory刪除這些路徑.
SQL> drop directory exp_dir;
Directory dropped
SQL> select * from dba_directories;
OWNER                          DIRECTORY_NAME                 DIRECTORY_PATH
------------------------------ ------------------------------ ------------------------------
SYS                            UTL_FILE_DIR                   /opt/oracle/utl_fileSYS                            
                               BDUMP_DIR                      /opt/oracle/admin/conner/bdump
 

create or replace directory USER_DIR as 'E:\PLSQL\310\';

DECLARE
   v_content VARCHAR2(1800);
   v_bfile BFILE;
   amount INT;
   offset INT :=1;
   
BEGIN
v_bfile := bfilename('USER_DIR','test.TXT'); -- 注意這裡的 User_dir 對應上面已經建立好啦的目錄
amount :=DBMS_LOB.getlength(v_bfile);
DBMS_LOB.OPEN(v_bfile);
DBMS_LOB.READ(v_bfile,amount,offset,v_content);
DBMS_LOB.close(v_bfile);
DBMS_OUTPUT.PUT_LINE(v_content);
END;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.