ORACLE UTL_FILE檔案包的應用,檔案I/O操作

來源:互聯網
上載者:User

利用ORACLE UTL_FILE包可以使文字檔轉入進資料表,反之亦然:

1: 建立檔案目錄

首先在資料庫伺服器上建立相應的檔案目錄。

1.1 方法:
在初始設定檔案設定檔init.ora的配置中將UTL_FILE_DIR = 'E:/temp'指定路徑;

1.2 方法:
建立路徑對象:Create directory test_dir as 'e:/temp'
建議用第二種方法;

2: 開啟和關閉檔案

所有的檔案控制代碼都擁有UTL_FILE.FILE_TYPE,FILE_TYPE在UTL_FILE規範中進行了定義。

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

2.2: FCLOSE(file_handle in out file_type);
唯一的參數是檔案控制代碼,就是關閉檔案;FCLOSE_ALL關閉所有檔案控制代碼;

2.3: IS_OPEN(file_handle in file_type) return Boolean;
判斷檔案是否開啟;

3: 檔案輸出函數:

3.1: PUT(file_handle in file_type, buffer in varchar2);
檔案輸出,但是不會在檔案中附加新行字元(newline),必須用put_line或者new_line向檔案中輸入終結符;

3.2: NEW_LINE(file_handle in file_type, lines in natural := 1) ;
向檔案中寫入一個或者多個行終結符;

3.3: PUT_LINE(file_handle in file_type, buffer in varchar2);
等價PUT後接著用NEW_LINE;

3.4: PUTF(file_handle in file_type, format in varchar2,
Arg1 in varchar2 default null,
Arg2 in varchar2 default null,
Arg3 in varchar2 default null,
Arg4 in varchar2 default null,
Arg5 in varchar2 default null)
和PUT類似,但是它允許輸出字串是帶格式的,格式字元中n是換行,%s被選擇性參數取代;
例如:
Declare
        v_outputfile utl_file.file_type;
        v_name varchar2(20) := 'scott';
begin
        v_outputfile := utl_file.fopen(..);
        utl_file.putf(v_outputfile,
                'hi there! n my name is %s,and I am a %s major.n',
                V_name,
                'Computer science');
        Fclose(v_outputfile);
end;
輸出檔案格式:
hi there!
my name is scott ,and I am a Computer science major.

3.5: 輸出檔案應用執行個體:
create or replace procedure p_mmr_new
(p_start_time out date,
p_end_time out date)
is
        v_file UTL_FILE.FILE_TYPE;
        v_string varchar2(100);
        v_error exception;
        v_i number;
        cursor cur_ms_no is
                select distinct substr(trim(a.ms_no),1,11)
                from msno.t_upload_msno a,
                msno.t_msno_black b
                where substr(a.ms_no,1,11) = b.ms_no(+)
                and b.ms_no is null;
        v_count number;
        v_ms_no varchar2(20);
begin
        p_start_time:=sysdate;
        v_count:=0;
        -- insert,'TEST_DIR'要大寫
        v_file := UTL_FILE.FOPEN('TEST_DIR','ivr170.txt', 'w',32767);
        open cur_ms_no ;
        loop
                fetch cur_ms_no into v_ms_no;
                exit when cur_ms_no%notfound;
                        UTL_FILE.PUT_line(v_file, v_ms_no);
                        v_count:=v_count+1;
                        if v_count>=5000 then
                                UTL_FILE.fflush(v_file);
                                --UTL_FILE.FCLOSE(v_file);
                                --v_file := UTL_FILE.FOPEN('TEST_DIR','v_ms_no.txt', 'a',32767);
                                v_count:=0;
                        end if;
        end loop;
        UTL_FILE.FCLOSE(v_file);
        close cur_ms_no;
        --close
        p_end_time:=sysdate;
EXCEPTION
        WHEN v_error Then
        UTL_FILE.FCLOSE(v_file);
        RETURN;
end;

4:檔案輸入

4.1 get_line(file_handle in file_type, buffer out varchar2);
從檔案中讀出資料;
樣本:
Declare
        V_newline varchar2(200);
begin
        v_filehandle := utl_file.fopen(p_filedir, p_filename, 'r');
        utl_file.get_line(vfilehandle, v_newline);
        insert into t1 (tip) values(v_newline);
end;

本文轉自:http://blog.csdn.net/alex197963/archive/2008/01/08/2030201.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.