Академический Документы
Профессиональный Документы
Культура Документы
================
UTL_FILE is PL/SQLPackage thatcanbeusedtowritethedatafromtablestofilesand
filestotables.
Generallywewilluse UTL_FILE packageforwritingdatafromtablestofiles.
ImportantqueriestocheckthedetailsaboutUTL_FILE Package.
SELECTowner,object_type,status
FROMall_objects
WHEREobject_name='UTL_FILE';
Note: Ifthepackagestatusisinvalid,recompilethepackage.
Ifthepackageisnotlisted,run{ORACLE_HOME}/rdbms/admin/utlfile.sqltocreateit.
SELECTgrantee,privilege
FROMall_tab_privs
WHEREtable_name='UTL_FILE';
Note: Alwaysgranteeshouldbeuserloggedinorpublic,Ifthisprivilegeismissing,then
loginasanadminuserand
Run GRANTEXECUTEonUTL_FILE
UTL_FILE_DIR:
===========
To know Available Directory Path Details:
Select*fromv$parameterwherename='utl_file_dir';
For Creation:
CREATEORREPLACEDIRECTORYTEST_DIRAS' /usr/tmp';
Createorreplacedirectoryutl_file_diras'D:\;
Wewillusefollowingthreefunctionstogeneratethefile.
1) Utl_File.fopen = toopen(or)Createthefile(Wecanopenmaximum50filesatone
session)
2) Utl_File.Put_line = ToTransferthedataintotheFile.
3) Utl_File.fclose = toclosetheFileafterDatatransfer.
1. Utl_File.fopen: Opensafileforinputoroutputwiththedefaultlinesize.
FOPENreturnsafilehandle,whichmustbeusedinallsubsequentI/Ooperationsonthe
file.
Syntax:
UTL_FILE.FOPEN
(
LocationINVARCHAR2,
FilenameINVARCHAR2,
Open_modeINVARCHAR2
Max_linesizeINBINARY_INTEGER
)
RETURNUTL_FILE.FILE_TYPE;
Parameters:
Location>DirectoryPathorOracleDirectoryName.
Note:
Thefilelocationmustbeanaccessibledirectory,asdefinedintheinstance's
initializationparameterUTL_FILE_DIR.
Filename>Nameofthefilewithextension(File_type).
Open_mode>Specifieshowfileistobeopened.(NotaCasesensitive)
R>ReadsText(get_line)
W>WritesText(put,put_line,putf,fflush)
A>AppendText(put,put_line,putf,fflush,new_line)
RB>ReadBinary
WB>WriteBinary
ABAppendBinary
Note:
Ifyouopenafileusingopen_mode=Abutthefilenotexistedthenitwillopen
(Created)inwautomatically.
Max_linesize>minimumvalue1,maximumvalue32767 (Wecanpassnullvaluealso)
Exceptions:
INVALID_PATH
INVALID_MODE
INVALID_OPERATION
INVALID_MAXLINESIZE
:
:
:
:
2 Utl_File.Put_line: It is used to write the data stored in buffer parameter to the open
file identified by the file handler. The file must be open for write operations.
PUT_LINE terminates the line with the platform-specific line terminator.
Syntax:
UTL_FILE.PUT_LINE
(
File
IN FILE_TYPE,
Buffer IN VARCHAR2
);
Parameters:
File >ActivefileHandlereturnedbyanUTL_FILE.FOPEN
Buffer >TextBufferthatcontainsthelinestobewritetothefile.
Exceptions:
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR
Parameters:
File >ActivefileHandlereturnedbyanUTL_FILE.FOPEN
Exceptions:
WRITE_ERROR
INVALID_FILEHANDLE
Note:
If there is buffered data yet to be written when FCLOSE runs, then you may receive
a WRITE_ERROR exception when closing a file.
UTL_FILE.IS_OPEN
Syntax:
UTL_FILE.IS_OPEN
(
File IN FILE_TYPE
)
RETURN BOOLEAN;
Parameters:
File >ActivefileHandlereturnedbyanUTL_FILE.FOPEN.
Returns:
TrueorFalse.
UTL_FILE.FCLOSE_ALL
It closes all open file handles for the session.
Syntax:
UTL_FILE.FCLOSE_ALL;
Exceptions:
WRITE_ERROR
Note:
FCLOSE_ALL does not alter the state of the open file handles held by the user. This means
that an IS_OPEN test on a file handle after an FCLOSE_ALL call still returns TRUE, even
though the file has been closed.
No further read or write operations can be performed on a file that was open before an
FCLOSE_ALL.
UTL_FILE.GET_LINE
Syntax:
UTL_FILE.GET_LINE
(
File
Buffer
IN FILE_TYPE,
OUT VARCHAR2
);
Parameters:
File >ActivefileHandlereturnedbyanUTL_FILE.FOPEN
Buffer >TextBuffertoreceivethelinesreadfromthefile.
Note:
File Must be opened in open_mode=R
Exceptions:
INVALID_OPERATION
INVALID_FILEHANDLE
READ_ERROR
NO_DATA_FOUND
VALUE_ERROR
Other Functions:
UTL_FILE.PUT
writes the text string stored in the buffer parameter to the open file identified by the file handle. The file
Must be open for write operations. No line terminator is appended by PUT use NEW_LINE to terminate the line
or
Use PUT_LINE to write a complete line with a line terminator.
PUT
Syntax:
UTL_FILE.PUT
(
File IN FILE_TYPE,
Buffer IN VARCHAR2
);
Exceptions:
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR
UTL_FILE.NEW_LINE
Syntax:
UTL_FILE.NEW_LINE
(
File IN FILE_TYPE,
Lines IN NATURAL: = 1
);
Parameters:
File >ActivefileHandlereturnedbyanUTL_FILE.FOPEN
Lines >Number oflineterminatorstobewrittentothefile.
Exceptions:
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR
****