使用Oracle UTL_FILE包操作文件
本文檔僅作為參考使用,如需要關于UTL_FILE包的官方詳細描述,請點擊這里,更多例子,請參考這里。
?
UTL_FILE封裝了PL/SQL程序中對文件的操作,通常被用于PL/SQL程序的日志記錄。
在使用之前,先必須確定好UTL_FILE包的參數設置是否正確,通常只需要設置‘utl_file_dir’參數就即可,‘utl_file_dir’定義了所操作文件的根目錄。
檢查參數
utl_file_dir的值可以有多個,設置多個目錄時使用逗號分隔符,在使用fopen函數的時候必須指定其中任何一個目錄即可,如果utl_file_dir只有一個可用目錄,則不需要指定(見后面的例子)
設置參數
使用如下的命令重置utl_file_dir的值,指定你所需要的目錄即可。
ALTER SYSTEM SET utl_file_dir='/tmp/','/oradata','/home/'
?
使用自定義目錄
如果不使用utl_file_dir參數設置目錄,也可是使用自定義目錄來定義文件讀寫根目錄,但必須對目錄擁有相應的權限,如果沒有則需要sysadm授權
CREATE?DIRECTORY oraload?AS?'c:\oraload\';
GRANT?READ,WRITE ON DIRECTORY oraload TO UWCLASS;
?
常用方法
utl_file.fopen(file_location IN?VARCHAR2,?file_name IN?VARCHAR2,?open_mode IN?VARCHAR2,?max_linesize? IN?BINARY_INTEGER?DEFAULT?NULL)?RETURN?file_type;
fopen方法打開一個文件,函數返回類型為utl_file.file_type的文件句柄。
參數:
file_location:文件路徑可以為utl_file_dir中的任何一個路徑,或者使用自定義路徑,在使用自定義路徑或者utl_file_dir(value中只有一個路徑)時,參數名必須大寫。utl_file.fopen('ORALOAD', ...)
? ? ? file_name:文件名
open_mode:文件打開模式,有一下幾種
A:Append 文本
AB:Append字節
R:文本只讀
RB:字節只讀
W:文本寫入
WB:字節寫入
常用的為A、R、W
max_linesize:每行字符數的最大值,最小為1,最大為32767,默認使用1024
?
utl_file.get_line(file???IN? FILE_TYPE,buffer?OUT?VARCHAR2,len IN??BINARY_INTEGER?DEFAULT?NULL);
getline方法從指定文件句柄中讀取一行數據,可以指定每次讀取內容的大小。
參數:
file:使用fopen打開的文件句柄
buffer:接受讀取內容的緩存
len:讀取內容的字節大小,默認將會使用最大值
?
UTL_FILE.PUT_LINE (file IN FILE_TYPE,?buffer IN VARCHAR2,?autoflush IN BOOLEAN DEFAULT FALSE);
向指定文件中寫入一行內容。
參數:
file:要寫入內容的目標文件句柄
buffer:需要寫入的內容
autoflash:是否在write之后將內容從緩沖區寫入磁盤
?
UTL_FILE.IS_OPEN (file IN FILE_TYPE)?RETURN BOOLEAN;
用來判斷指定文件句柄是否為open狀態,返回類型為布爾值
?
UTL_FILE.FCLOSE (file IN OUT FILE_TYPE);
用來關閉一個已經打開的文件句柄,在使用完文件后,必須關閉文件句柄。
?
例子
下面這段代碼講會從指定文件中讀取數據庫表名,然后將表的部分信息以html格式輸出到文件中。
1 /* 2 Before running this script, please use command 'show parameter utl_file' to check 'utl_file_dir' parameter, 3 and then set the v_log_path with one of the value of the 'utl_file_dir' values. Please inform your sysdba to 4 set the 'utl_file_dir' parameter. 5 */ 6 SET serveroutput ON; 7 DECLARE 8 v_log_path VARCHAR2(1000) DEFAULT '/usr/tmp'; 9 v_log_file_appender VARCHAR2(1000) DEFAULT ''; 10 v_health_check_tables_file utl_file.file_type; 11 v_health_check_file_name varchar2(100) default 'health_check_tables.txt'; 12 v_perf_log utl_file.file_type; 13 v_flag_column_info boolean default false; 14 15 v_table_name VARCHAR2(100) DEFAULT 'MTL_SYSTEM_ITEMS_B'; 16 v_data_vol_count NUMBER DEFAULT 0; 17 v_data_vol_query_str VARCHAR2(1000); 18 v_table_stats_record all_tables%rowtype; 19 v_table_stats_record_num NUMBER DEFAULT 0; 20 v_column_stats_record all_tab_columns%rowtype; 21 v_index_stats_record all_indexes%rowtype; 22 23 CURSOR v_cursor_column_rec(p_table_name VARCHAR2) IS 24 SELECT * FROM all_tab_columns WHERE table_name = p_table_name ORDER BY LAST_ANALYZED ASC; 25 26 CURSOR v_cursor_index_rec(p_table_name VARCHAR2) IS 27 select * from all_indexes where table_name = p_table_name order by last_analyzed asc; 28 BEGIN 29 v_log_file_appender := to_char(sysdate, 'yyyy-mm-dd') || '_' || to_char(sysdate, 'SSSSS'); 30 --open log file 31 v_perf_log := utl_file.fopen(v_log_path, 'pim_healthcheck_'|| v_log_file_appender ||'.html', 'A'); 32 IF (utl_file.is_open(v_perf_log)) THEN 33 v_health_check_tables_file := utl_file.fopen(v_log_path, v_health_check_file_name, 'R'); 34 end if; 35 36 loop 37 BEGIN 38 utl_file.get_line(v_health_check_tables_file, v_table_name); 39 v_table_name := REPLACE(v_table_name, chr(10), ''); 40 v_table_name := REPLACE(v_table_name, chr(13), ''); 41 v_table_name := upper(rtrim(ltrim(v_table_name))); 42 IF (v_table_name IS NOT NULL) THEN 43 utl_file.put_line(v_perf_log, '<h2>Table ' || v_table_name || '</h2>'); 44 -- data volumn check 45 v_data_vol_query_str := 'select count(*) from ' || v_table_name; 46 EXECUTE IMMEDIATE v_data_vol_query_str INTO v_data_vol_count; 47 utl_file.put_line(v_perf_log, 'The number of rows in table ' || v_table_name || ' is ' || to_char(v_data_vol_count)); 48 -- table static information 49 SELECT count(*) INTO v_table_stats_record_num FROM all_tables WHERE table_name = v_table_name; 50 utl_file.put_line(v_perf_log, '<table border=1>'); 51 utl_file.put_line(v_perf_log, '<tr><th>Last Analyze Date</th><th>NUM_ROWS</th><th>BLOCKS</th><th>AVG_ROW_LEN</th></tr>'); 52 IF (v_table_stats_record_num > 0) THEN 53 SELECT * INTO v_table_stats_record FROM all_tables WHERE table_name = v_table_name AND ROWNUM = 1; 54 utl_file.put_line(v_perf_log, '<tr>'); 55 utl_file.put_line(v_perf_log, '<td>' || to_char(v_table_stats_record.last_analyzed, 'yyyy/mm/dd HH24:MI:SS') || '</td>'); 56 IF (v_table_stats_record.num_rows < v_data_vol_count) THEN 57 utl_file.put_line(v_perf_log, '<td>' || to_char(v_table_stats_record.num_rows) || '</td>'); -- can mark alert info 58 ELSE 59 utl_file.put_line(v_perf_log, '<td>' || to_char(v_table_stats_record.num_rows) || '</td>'); 60 END IF; 61 utl_file.put_line(v_perf_log, '<td>'||to_char(v_table_stats_record.BLOCKS)||'</td>'); 62 utl_file.put_line(v_perf_log, '<td>'||to_char(v_table_stats_record.avg_row_len)||'</td>'); 63 utl_file.put_line(v_perf_log, '</tr>'); 64 END IF; 65 utl_file.put_line(v_perf_log, '</table>'); 66 -- table definition area 67 --1. column definition 68 IF (v_flag_column_info) THEN 69 utl_file.put_line(v_perf_log, '<h3>Column information</h3>'); 70 utl_file.put_line(v_perf_log, '<table border=1>'); 71 utl_file.put_line(v_perf_log, '<tr><th>COLUMN_NAME</th><th>DATA_TYPE</th><th>DATA_LENGTH</th><th>LAST_ANALYZED</th></tr>'); 72 OPEN v_cursor_column_rec(v_table_name); 73 loop 74 fetch v_cursor_column_rec INTO v_column_stats_record; 75 utl_file.put_line(v_perf_log, '<tr>'); 76 utl_file.put_line(v_perf_log, '<td>'||to_char(v_column_stats_record.COLUMN_NAME)||'</td>'); 77 utl_file.put_line(v_perf_log, '<td>'||to_char(v_column_stats_record.DATA_TYPE)||'</td>'); 78 utl_file.put_line(v_perf_log, '<td>'||to_char(v_column_stats_record.DATA_LENGTH)||'</td>'); 79 utl_file.put_line(v_perf_log, '<td>'||to_char(v_column_stats_record.LAST_ANALYZED)||'</td>'); 80 utl_file.put_line(v_perf_log, '</tr>'); 81 exit when v_cursor_column_rec%NOTFOUND; 82 END loop; 83 CLOSE v_cursor_column_rec; 84 end if; 85 -- 2. index information 86 utl_file.put_line(v_perf_log, '<h3>Index information</h3>'); 87 utl_file.put_line(v_perf_log, '<table border=1>'); 88 utl_file.put_line(v_perf_log, '<tr><th>INDEX_NAME</th><th>INDEX_TYPE</th><th>LAST_ANALYZED</th><th>NUM_ROWS</th></tr>'); 89 OPEN v_cursor_index_rec(v_table_name); 90 loop 91 fetch v_cursor_index_rec INTO v_index_stats_record; 92 utl_file.put_line(v_perf_log, '<tr>'); 93 utl_file.put_line(v_perf_log, '<td>'||to_char(v_index_stats_record.INDEX_NAME)||'</td>'); 94 utl_file.put_line(v_perf_log, '<td>'||to_char(v_index_stats_record.INDEX_TYPE)||'</td>'); 95 utl_file.put_line(v_perf_log, '<td>'||to_char(v_index_stats_record.LAST_ANALYZED)||'</td>'); 96 utl_file.put_line(v_perf_log, '<td>'||to_char(v_index_stats_record.NUM_ROWS)||'</td>'); 97 utl_file.put_line(v_perf_log, '</tr>'); 98 exit when v_cursor_index_rec%NOTFOUND; 99 END loop; 100 utl_file.put_line(v_perf_log, '</table>'); 101 CLOSE v_cursor_index_rec; 102 end if; 103 exception 104 WHEN NO_DATA_FOUND THEN 105 exit; 106 END; 107 end loop; 108 109 utl_file.fclose(v_perf_log); 110 utl_file.fclose(v_health_check_tables_file); 111 EXCEPTION 112 WHEN UTL_FILE.INVALID_PATH THEN 113 -- error during opening log file 114 dbms_output.put_line('Error: please check your "utl_file_dir" setting first.'); 115 WHEN OTHERS THEN 116 IF (utl_file.is_open(v_perf_log)) THEN 117 -- close file if there is any exception; 118 utl_file.fclose(v_perf_log); 119 END IF; 120 IF (utl_file.is_open(v_health_check_tables_file)) THEN 121 utl_file.fclose(v_health_check_tables_file); 122 end if; 123 END; 文件health_check_tables.txt的格式為:Table_Name_A
Table_Name_B
轉載于:https://www.cnblogs.com/fanzaoyang/archive/2012/08/30/oracle_utl_file_package_introduce.html
總結
以上是生活随笔為你收集整理的使用Oracle UTL_FILE包操作文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp.net MVC 中使用dataa
- 下一篇: Popupwin结合Timer实现定时弹