Professional Documents
Culture Documents
Utl File Package
Utl File Package
Slide No 1
Slide No 3
Security Aspects
UTL_FILE is Available For Both Client-Side And ServerSide PL/SQL.
The Client Operating in Text I/O Mode is Subject To
Normal Operating System File Permission Checking.
The Server Implementation May Be Running in A
Privileged Mode, Which Requires A Restriction on The
That
The End
User
CanKumar
Access.
Sunday,Directories
December 11, 2016
Introduction
External
Files by Satish
Yellanki
Slide No 4
Step 1
Create The Directory OR Folders on The Actual Operating
System Server.
The Folders OR Subfolders Should Be Created As Per The
Relative Path That is Required As Per The Project.
Step 2
Make The Oracle Database Know on Which Directory We
Are Working For Reading OR Writing Files.
Once We Specify The Working Directory For File
Operations We Should Operate Only From That Specified
Directory.
Creating The Directory
Syntax
SQL> CREATE DIRECTORY DirectoryName
AS DriveName:\FolderName;
Note
The Above Command Creates A New Directory Entry into The
Oracle Database With A Logical Name To The Physical Directory
That is Created in The Operating System.
The Logical Name From The Oracle Database Will Be Pointing To
The Physical Path of The Derve And Folder Where The File is
Stored.
Sunday, December 11, 2016
Introduction External Files by Satish Kumar Yellanki
Slide No 5
Slide No 6
Step 4
Create A New Text File Within The Physical Folders That
Are Created.
Step 5
Locating The File in The Physical Folders, We Read The
File Using PL/SQL Program.
Illustrated Example
DECLARE
MyFile UTL_FILE.FILE_TYPE;
V_FileOutput VARCHAR2(1000);
BEGIN
MyFile := UTL_FILE.FOPEN(
'SKYESSUTLFILE',
'PLSQL12_3.SQL', R
);
UTL_FILE.GET_LINE(MyFile , V_FileOutput);
UTL_FILE.FCLOSE(MyFile);
DBMS_OUTPUT.PUT_LINE(V_FileOutput);
END;
Note
The Above Code Identifies The Actual Physical Directory Under
The OS And The Corresponding Text File To Be Read And Prints
The First Line of The Actual Text File.
Sunday, December 11, 2016
Slide No 7
"UTL_FILE.FILE_TYPE Datatype
FILE_TYPE is A Data Type Which Points To A Particular File
Specified By The User.
Once The FILE_TYPE Successfully Points Towards The
Specific File Then We Can Proceed With Other Operations
Like Reading And Writing Upon The File.
Declarative Syntax
TYPE FILE_TYPE
IS RECORD (
ID BINARY_INTEGER,
DataType BINARY_INTEGER,
Byte_Mode BOOLEAN
);
Slide No 8
Subprogram
Description
FOPEN Function
FOPEN_NCHAR Function
IS_OPEN Function
FCLOSE Procedure
Closes A File.
FCLOSE_ALL Procedure
GET_LINE Procedure
GET_LINE_NCHAR Procedure
PUT Procedure
PUT_NCHAR Procedure
PUT_LINE_NCHAR Procedure
PUTF Procedure
GET_RAW Function
PUT_RAW Function
NEW_LINE Procedure
PUT_LINE Procedure
PUTF_NCHAR Procedure
Sunday, December 11, 2016
Slide No 9
Subprogram
FFLUSH Procedure
FSEEK Function
FREMOVE Function
FCOPY Function
FGETPOS Function
FGETATTR Procedure
FRENAME Function
Description
Assuming
That
We
Have
Points To Note
The File Location And File Name Parameters Are Supplied To The FOPEN
Function As Separate Strings, So That The File Location Can Be Checked
Against The List of Accessible Directories As Specified By The
ALL_DIRECTORIES View. The File Location And Name Must Represent A
Legal Filename on The System, And The Directory Must Be Accessible.
Slide No 10
FOPEN Function
FOPEN Function Opens A File.
We Can Specify The Maximum Line Size And Have A
Maximum of 50 Files Open Simultaneously.
Syntax
UTL_FILE.FOPEN(
Location IN VARCHAR2,
FileName IN VARCHAR2,
Open_Mode IN VARCHAR2,
Max_Linesize IN BINARY_INTEGER
) RETURN File_Type;
Append Text
AB
Append Byte Mode
R
Read Text
RB
Read Byte Mode
W
Write Text
WB December
Write
ByteIntroduction
Mode External Files by Satish Kumar Yellanki
Sunday,
11,
2016
Slide No 11
Security Model
Execute is Granted To PUBLIC Which is A Security Risk.
Revoke Execute Grant on PUBLIC After Installation.
Steps To Revoke
SQL> CONN SYS AS SYSDBA
SQL> REVOKE EXECUTE ON UTL_FILE FROM PUBLIC;
Exceptions
IS_OPEN Function
IS_OPEN Function Tests A File Handle To See if it
Identifies An Open File.
IS_OPEN Reports Only Whether A File Handle Represents
A File That Has Been Opened, But Not Yet Closed.
IS_OPEN Function Does Not Guarantee That There Will
Be No Operating System Errors When We Attempt To Use
The File Handle.
IS_OPEN Function Returns A Boolean Value of TRUE OR
FALSE, Returns TRUE if The File is Open.
Sunday, December 11, 2016
Slide No 12
Syntax
UTL_FILE.IS_OPEN(
File IN FILE_TYPE
) RETURN BOOLEAN;
FCLOSE Procedure
FCLOSE Procedure Closes An Open File Identified By A
File Handle.
If There is Buffered Data Yet To Be Written When
FCLOSE Runs, Then We May Receive A WRITE_ERROR
Exception When Closing A File.
Syntax
UTL_FILE.FCLOSE(
File IN OUT FILE_TYPE
);
Exceptions
WRITE_ERROR There is Buffered Data Yet To Be Written
INVALID_FILEHANDLE The File To Be Closed is Not Available
FCLOSE_ALL Procedure
FCLOSE_ALL Procedure Closes All Open File Handles For
The Session.
FCLOSE_ALL Should Be Used As An Emergency Cleanup
Procedure.
Sunday, December 11, 2016
Introduction External Files by Satish Kumar Yellanki
Slide No 13
Exceptions
WRITE_ERROR
GET_LINE Procedure
GET_LINE Procedure Reads Text From The Open File
Identified By The File Handle And Places The Text in The
Output Buffer Parameter.
Text is Read Up To, But Not Including, The Line
Terminator, OR Up To The End of The File, OR Up To The
End of The LINESIZE Parameter.
It Cannot Exceed The MAX_LINESIZE Specified in FOPEN.
If The Line Does Not Fit in The Buffer, Then A
VALUE_ERROR Exception is Raised.
Sunday, December 11, 2016
Slide No 14
Exceptions
File
IN FILE_TYPE,
Buffer
OUT VARCHAR2,
LineSize IN NUMBER,
Len
IN PLS_INTEGER DEFAULT NULL
);
INVALID_FILEHANDLE
INVALID_OPERATION
READ_ERROR
NO_DATA_FOUND
VALUE_ERROR
GET_RAW Function
GET_RAW
Reads
A RAW
String
Sunday,
December 11, 2016 Function
Introduction
External Files
by Satish Kumar
YellankiValue From File.
Slide No 15
PUT Procedure
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 Function, We
Should Use NEW_LINE To Terminate The Line OR Use
PUT_LINE To Write A Complete Line.
The Maximum Size of The Buffer Parameter is 32767
Bytes Unless We Specify A Smaller Size in FOPEN.
The Default is Approximately 1000 Bytes, Depending on
The Platform.
The Sum of All Sequential PUT Calls Cannot Exceed
32767 Without Intermediate Buffer Flushes.
Slide No 16
Syntax
UTL_FILE.PUT(
Exceptions
File
IN FILE_TYPE,
Buffer IN VARCHAR2
);
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR
PUT_RAW Function
PUT_RAW Function Accepts As Input A RAW Data Value
And Writes The Value To The Output Buffer.
We Can Request An Automatic Flush of The Buffer By
Setting The Third Argument To TRUE.
The Maximum Size of The Buffer Parameter is 32767
Bytes Unless We Specify A Smaller Size in FOPEN.
The Default is Approximately 1000 Bytes, Depending on
The Platform.
The Sum of All Sequential PUT Calls Cannot Exceed
32767 Without Intermediate Buffer Flushes.
The Raw Data Accepted By The PUT_RAW Function Can
Be Either Audio, Video OR Picture.
Sunday, December 11, 2016
Slide No 17
Syntax
UTL_FILE. PUT_RAW(
Exceptions
FID
IN UTL_FILE.FILE_TYPE,
R
IN RAW,
AutoFlush IN BOOLEAN DEFAULT FALSE
);
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR
PUT_LINE Procedure
PUT_LINE Procedure 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.
PUT_LINE Terminates The Line With The PlatformSpecific Line Terminator Character OR Characters.
The Maximum Size of The Buffer Parameter is 32767
Bytes Unless We Specify A Smaller Size in FOPEN.
The Default is Approximately 1000 Bytes, Depending on
The Platform. The Sum of All Sequential PUT Calls
Cannot Exceed 32767 Without Intermediate Buffer
Sunday, December 11, 2016
Introduction External Files by Satish Kumar Yellanki
Slide No 18
Flushes.
Syntax
UTL_FILE.PUT_LINE(
Exceptions
File
IN FILE_TYPE,
Buffer IN VARCHAR2,
AutoFlush IN BOOLEAN DEFAULT FALSE
);
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR
PUTF Procedure
PUTF Procedure is A Formatted PUT Procedure.
PUTF Works Like A Limited printf() Function.
The Format String Can Contain Any Text, But The
Character Sequences %s And \n Have Special Meaning.
Syntax
UTL_FILE.PUTF(
File IN FILE_TYPE,
Format IN VARCHAR2,
[Arg1 IN VARCHAR2 DEFAULT NULL,
...
Arg5 IN VARCHAR2 DEFAULT NULL]
);
Introduction External Files by Satish Kumar Yellanki
Slide No 19
Exceptions
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR
FFLUSH Procedure
FFLUSH Physically Writes Pending Data To The File
Identified By The File Handle And Data Being Written To
A File is Buffered.
The FFLUSH Procedure Forces The Buffered Data To Be
Written To The File.
The Data Must Be Terminated With A Newline Character.
Flushing is Useful When The File Must Be Read While Still
Open.
Debugging Messages Can Be Flushed To The File So That
They Can Be Read Immediately.
FFLUSH Function is Equivalent To The fflush() Function
in Language Like C Language.
Syntax
UTL_FILE.FFLUSH(
File IN FILE_TYPE
);
Invalid_MaxLineSize EXCEPTION;
Sunday, December 11, 2016
Slide No 20
Exceptions
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR
FSEEK Function
This Function Adjusts The File Pointer Forward OR
Backward Within The File By The Number of Bytes
Specified.
If Offset is Provided Then The Function Seeks To A Byte
Offset.
If The End of The File OR The Beginning of The File is
Reached Before Seeking is Done, The Function Returns
The Last OR First Row, Respectively.
If Location is Provided Then The Function Seeks To An
Absolute Location Specified in Bytes.
Syntax
UTL_FILE.FSEEK(
FID
IN UTL_FILE.FILE_TYPE,
Absolute_Offset IN PL_INTEGER DEFAULT NULL,
Relative_Offset IN PLS_INTEGER DEFAULT NULL
);
Slide No 22
Location IN VARCHAR2,
FileName IN VARCHAR2,
Dest_Dir IN VARCHAR2,
Dest_File IN VARCHAR2,
Start_Line IN PLS_INTEGER DEFAULT 1,
End_Line IN PLS_INTEGER DEFAULT NULL
);
FGETPOS Function
This Function Returns The Current
Position Within A File, in Bytes.
Syntax
UTL_FILE.FGETPOS(
FileID IN FILE_TYPE
) RETURN PLS_INTEGER;
Relative
Offset
FGETATTR Procedure
FGETATTR Procedure Reads And Returns The Attributes
of A Disk File.
Syntax
UTL_FILE.FGETATTR(
Location IN VARCHAR2,
Filename IN VARCHAR2,
Exists
OUT BOOLEAN,
File_Length OUT NUMBER,
BlockSize OUT NUMBER
);
FRENAME Function
FRENAME Function Renames An Existing File To A New
Name, Similar To The Unix MV Function.
Permission on Both The Source And Destination
Directories Must Be Granted.
We Can Use The Overwrite Parameter To Specify
Whether OR Not To Overwrite A File if One Exists in The
Destination Directory. The Default is FALSE For No
Overwrite.
FRENAME Renames The File Permanently Hence Any
Dependencies on The Previous File May Not Operate
Sunday, December 11, 2016
Slide No 24
Syntax
UTL_FILE.FRENAME(
Location IN Varchar2,
FileName IN VARCHAR2,
Dest_DIR IN VARCHAR2,
Dest_File IN VARCHAR2,
Overwrite IN BOOLEAN DEFAULT FALSE
);
Pseudo Syntax
In Windows NT
FILE_ID := UTL_FILE.FOPEN(
In UNIX
FILE_ID := UTL_FILE.FOPEN(
'K:\ SkyEssDB\Debug'
'Trace.Lis',
'R
);
'/Usr/SkyEssDB/Admin',
'Trace.Lis',
'W
);
Note
We Can Read Directly From A File into A Numeric OR Date
Buffer, But in This Case, The Data in The File Will Be Converted
Implicitly, And So it Must Be Compatible With The Datatype of
The Buffer.
Exceptions
Exception Name
Error Code
Reason
Access_Denied
29289
CharSetMisMatch
29298
Delete_Failed
29291
File_Open
Internal_Error
29286
Invalid_FileHandle
29282
Invalid_FileName
29288
A File With The Specified Name Does Not Exist in The Path
Invalid_MaxLineSize
29287
Invalid_Mode
29281
Invalid_Offset
29290
Invalid_Operation
29283
Invalid_Path
29280
Read_Error
29284
Rename_Failed
29292
Write_Error
29285
Slide No 29