Using UTL_FILE Package To Perform File I/O (UNIX) And Basic FAQ_44307.1
Using UTL_FILE Package To Perform File I/O (UNIX) And Basic FAQ (Doc ID 44307.1)
In this Document
Applies to:PL/SQL - Version 7.3.4.5 to 11.2.0.1.0Information in this document applies to any platform. Checked for relevance on 3-MAR-2012. PurposeA Quick Overview of the UTL_FILE package ScopeThe UTL_FILE package is available to read and write to operating system flat files.
The UTL_FILE package is available on all platforms, but this article focuses on the UNIX platform only. DetailsIntroduction
The UTL_FILE package is available to read and write to operating system flat files. The UTL_FILE package is available on all platforms, but this article focuses on the UNIX platform only. The package procedures and functions are described in details in the documentation Oracle Database PL/SQL Packages and Types Reference PermissionsPre 9iR2 (9.2)
UTL_FILE used it's own mechanism to specify which files were accessible via the UTL_FILE_DIR parameter in the init If you list multiple values, all entries of this parameter must be on contiguous lines of the parameter file. If you separate them with other parameters, Oracle will read only the last (contiguous) lines. The number of directories that can be listed is limited by the 255 character buffer size.
All users can read or write to all files specified by this parameter. Therefore all PL/SQL users must be trusted with the information in the directories specified by this parameter.
ALTER SYSTEM SET utl_file_dir= '
Starting from 9iR2 As of 9.2, this functionality was expanded so that you were able to specify an Oracle DIRECTORY object instead of the exact directory name, thus avoiding a lot of hard coding.
Each directory to be accessed by UTL_FILE can be specified via the CREATE DIRECTORY command. And specified via this level of indirection in UTL_FILE.FOPEN. Care must be taken to use uppercase for the DIRECTORY object in the call to utl_file.fopen, else you may encounter the ORA-29280: invalid directory path error. Note: In both cases (pre and post 9iR2), the directories that UTL_FILE can read from and write to must be accessible by the Oracle instance owner since the shadow processes run as that user By default, Oracle creates the file, then the permissions on it are rw-r--r-- To give read/write permissions to all users on the directory, use chmod command.
i.e chmod 777 Example Usage
The following is an example demonstrating how to set up your environment, and includes a small PL/SQL procedure that writes out to an operating system flat file.
create or replace directory UTF_DIR as '/home/usupport/forums';
drop table TESTTAB;
create table TESTTAB ( C1 NUMBER, C2 NUMBER ) / insert into TESTTAB values(10,25); insert into TESTTAB values(20,50); commit;
create or replace procedure UTL_FILE_EXAMPLE is FILE_HANDLE utl_file.file_type; -- file handle of OS flat file COL1 number; -- C1 retrieved from testtab table RETRIEVED_BUFFER varchar2(100); -- Line retrieved from flat file -- The old form is included for reference: -- DIR_NAME varchar2(40) := '/home/usupport/forums'; -- But this example uses the DIRECTORY created above. DIR_NAME varchar2(40) := 'UTF_DIR'; FILE_NAME varchar2(40) := 'myfile.txt'; begin -- Open file to write into and obtain its file_handle. FILE_HANDLE := utl_file.fopen(DIR_NAME,FILE_NAME,'W'); -- Write a line of text out to the file. utl_file.put_line(FILE_HANDLE, 'This is line 1 as a test'); -- Select the c1 column from the testtab table where C2 is 25. select C1 into COL1 from TESTTAB where C2 = 25; -- Using PUTF write text with the col1 argument out to the file. utl_file.putf ( FILE_HANDLE, 'C1 is %s when C2 is %s.\n', COL1, '25' ); -- Close the file. utl_file.fclose(FILE_HANDLE); -- Open the same file to read from. file_handle := utl_file.fopen(DIR_NAME,FILE_NAME,'R'); -- Read a line from the file. utl_file.get_line (FILE_HANDLE, RETRIEVED_BUFFER); -- Print fetched line out to the SQL*Plus prompt. dbms_output.put_line(RETRIEVED_BUFFER); -- Close the file. utl_file.fclose(FILE_HANDLE); exception when no_data_found then dbms_output.put_line('No Data Found'); utl_file.fclose(FILE_HANDLE); when utl_file.invalid_path then dbms_output.put_line('UTL_FILE.INVALID_PATH'); utl_file.fclose(FILE_HANDLE); when utl_file.read_error then dbms_output.put_line(' UTL_FILE.READ_ERROR'); utl_file.fclose(FILE_HANDLE); when utl_file.write_error then dbms_output.put_line('UTL_FILE.WRITE_ERROR'); utl_file.fclose(FILE_HANDLE); when utl_file.invalid_operation then dbms_output.put_line('UTL_FILE.INVALID_OPERATION'); utl_file.fclose(FILE_HANDLE); when others then dbms_output.put_line('Unhandled Error : '||sqlcode); dbms_output.put_line(sqlerrm); utl_file.fclose(FILE_HANDLE); end; /
SQL> set serverout on
SQL> exec utl_file_example This is line 1 as a test PL/SQL procedure successfully completed.
Frequently asked Questions and Answers:
Q: I have added the directory path to my init MORE ARTICLES ON UTL_FILE
Note.196939.1 Using CREATE DIRECTORY Instead of UTL_FILE_DIR init.ora Parameter
Collaborate with and learn from your Peers, Industry Experts and Oracle Support Product Specialists using My Oracle Support Community. Join us here: References |
|
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17252115/viewspace-1256729/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Using Create directory & UTL_FILE in OracleOracle
- ORACLE UTL_FILE檔案包的應用,檔案I/O操作Oracle
- hp unix 硬碟I/O分析硬碟
- IRP(I/O Request Package)詳解Package
- [Oracle Script] check File I/OOracle
- Master Note For PL/SQL UTL_FILE Package (Doc ID 1155024.1)ASTSQLPackage
- goldengate extract abended unable to queue I/O, I/O beyond file sizeGo
- oracle utl_fileOracle
- 等待事件:Disk file operations I/O事件
- 【UTL_FILE】使用UTL_FILE包生成檔案並寫入資料
- Export with Spool and Parallel Utl_FileExportParallel
- Retrieve deleted files on Unix / Linux using File Descriptors [ID 444749.1]deleteLinux
- db file async I/O submit 等待事件優化MIT事件優化
- db file async I/O submit 等待事件說明MIT事件
- utl_file包的應用
- UTL_FILE遍歷檔案
- [Javascript] Perform Set Operations using JavaScript Set MethodsJavaScriptORM
- Unix Sed Tutorial: Delete File Lines Using Address and Patternsdelete
- Oracle - UTL_FILE包之詳解Oracle
- UTL_FILE包的簡單例子單例
- Java I/O系統學習系列一:File和RandomAccessFileJavarandomMac
- db file async I/O submit等待事件的故障診斷MIT事件
- Veritas Quick I/O and Cached Quick I/OUI
- Basic LINUX/UNIX Commands(轉)Linux
- 計算機I/O與I/O模型計算機模型
- I/O埠和I/O記憶體記憶體
- Oracle內建包UTL_FILE使用說明Oracle
- clob utl_file 匯出成TXT檔案
- I/O上的等待事件 —— control file sequential read/control file parallel write事件Parallel
- zt_Limiting I/O and CPU resources using 11g Oracle Resource ManagerMITOracle
- Java I/OJava
- oracle利用utl_file包來讀寫檔案Oracle
- python unix :No such file or directoryPython
- Oracle - UTL_FILE包之BLOB匯入和匯出Oracle
- 使用utl_file做選擇性資料匯出
- Oracle 11g UTL_FILE 包的使用方法Oracle
- Java(8)I/OJava
- 【java】I/O流Java