0% found this document useful (0 votes)
267 views6 pages

UTL File Package

The UTL_FILE package allows writing data from tables to files and files to tables in PL/SQL. It uses Oracle directories to reference operating system directories. The package contains functions like FOPEN to open a file, PUT_LINE to write a line to a file, FCLOSE to close a file, and GET_LINE to read a line from a file. Administrative users must grant privileges to other users to access directories and use the package functions.

Uploaded by

chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
267 views6 pages

UTL File Package

The UTL_FILE package allows writing data from tables to files and files to tables in PL/SQL. It uses Oracle directories to reference operating system directories. The package contains functions like FOPEN to open a file, PUT_LINE to write a line to a file, FCLOSE to close a file, and GET_LINE to read a line from a file. Administrative users must grant privileges to other users to access directories and use the package functions.

Uploaded by

chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

UTL_FILE Package:

================
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';

Grant a user access to the directory:


GRANTread,writeONDIRECTORYTEST_DIRTOAPPS;

To know Directory Path Details:


SELECTdirectory_name,directory_path
FROMall_directories
WHEREDIRECTORY_NAME='TEST_DIR';

To know Directory Path Priviliges:


SELECTgrantee,privilege
FROMall_tab_privs
WHEREtable_name='TEST_DIR';
Note:
UTL_FILEusesOracledirectories,notOSdirectorieslikeD:\
InsteadcreateoracledirectorywithreferencetoanOSDirectorylike

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

:
:
:
:

File location or name was invalid.


The open_mode string was invalid.
File could not be opened as requested.
Specified max_linesize is too large or too small.

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

: File could not be opened as requested.


: File handle was invalid.

WRITE_ERROR

: Operating system error occurred during the write operation.

3 Utl_File.fclose: It closes an open file identified by a file handle.


Syntax:
UTL_FILE.FCLOSE
(
File IN OUT FILE_TYPE
);

Parameters:
File >ActivefileHandlereturnedbyanUTL_FILE.FOPEN

Exceptions:
WRITE_ERROR
INVALID_FILEHANDLE

: Operating system error occurred during the write operation.


: File could not be opened as requested.

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:

: Operating system error occurred during the write operation.

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

: File could not be opened as requested.


: File could not be opened as requested.
: Operating system error occurred during the read operation.

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

: File could not be opened as requested.


: File handle was invalid.

WRITE_ERROR

: Operating system error occurred during the write operation.

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

: File could not be opened as requested.


: File handle was invalid.

WRITE_ERROR

: Operating system error occurred during the write operation.

****

UTL_FILE.PUT (v_empfile_tgt, v_empline);


UTL_FILE.NEW_LINE (v_empfile_tgt, 2);

You might also like