Previous | Table of Contents | Next

Page 425

Day 18

Writing to Files and the Display

by Timothy Atwood

By default, Oracle does not have any functional input and output processes when using PL/SQL. However, Oracle provides several packages that perform input and output. Today's lesson discusses the following packages:

Page 426

UTL_FILE Definition

The UTL_FILE package is a very powerful and flexible package that enables you to work with files on both the client side and server side; you can work with files on different operating systems. This can be a scary proposition on the server side unless proper security is in place. You can specify the only directories that can be written to by the UTL_FILE package with the utl_file_dir parameter located in the INIT.ORA Oracle initialization file. If you do not have this line in your initialization file, add the statement utl_file_dir = directory_name, where directory_name is the directory location where the files are stored.

You could then limit access to a temp directory on the server (assuming Personal Oracle) by specifying

INPUT
utl_file_dir = c:\temp

NOTE
If the operating system is case sensitive, then the subdirectories must be assigned with the same case as the actual directory. (/tmp is different in UNIX than /Tmp.)

In addition, if you want to give write permission to all directories, you would simply specify

INPUT
utl_file_dir = *

NOTE
Be careful with permissions granted to directories. They can overwrite operating system permissions, thereby giving access to users who normally wouldn't be able to access files in those directories.

When an Oracle user creates a file, all rights and permissions to the file are assigned to the user. If other users need access to those files, it is up to the security administrator to change the permissions on the files.

File Input

The UTL_FILE package enables you to read into Oracle files located on the server or on the client. You could easily convert ASCII to EBCDIC files between the client and the server, utilizing some of the Oracle functions you learned on Day 7, "Using Oracle's Built-in Functions," and in Appendix B, "Oracle Functions Reference." Other possibilities include

Page 427

As you can see, this gives you tremendous flexibility when exchanging files between platforms, applications, and so on with Oracle. The process to read in a file is

  1. Assign a file handle to the file. This creates a pointer to the file to provide information to the operating system such as whether the file is in use, and where the user is currently located in the file for reading and writing operations.
  2. Declare a string of type VARCHAR2 to act as a buffer for reading in the file one line at a time.
  3. Use FOPEN to open the file in read mode, as specified by the parameter R.
  4. Use GET_LINE to read in data from the file into your declared buffer of type VARCHAR2, one line at a time.
  5. Close the file and release the file handle by using FCLOSE.

The next few sections examine the details of reading a file into Oracle.

Declaring a File Handle
When you open a file with the UTL_FILE package, a file handle is returned with a datatype of UTL_FILE.FILE_TYPE.

The Syntax for Creating a File Handle
The format to create the file handle is

DECLARE
     v_READFILE UTL_FILE.FILE_TYPE;
BEGIN

The FILE_TYPE is a PL/SQL record, which contains the necessary information about the file you are attempting to read or write including the filename, file location, method of accessing the file, and so on.

Using FOPEN to Open a File
FOPEN opens a file for reading or writing only. Table 18.1 provides a listing of possible exceptions raised from the function call. You cannot perform both operations on the same file simultaneously.

Page 428

The Syntax for the FOPEN Function
FOPEN is defined as

FUNCTION FOPEN(location IN VARCHAR2,
                    filename IN VARCHAR2,
                    openmode IN VARCHAR2)
RETURN UTL_FILE.FILE_TYPE;

The first parameter location is a VARCHAR2 string, which holds the location (path) of the file. This can be restricted by utl_file_dir as mentioned earlier in this chapter. The second parameter, filename, is the actual filename itself. The third parameter, openmode, defines how you will access the file.

You can access the file as

For file input, you will use only mode `R'. The function then returns the file handle of type UTL_FILE.FILE_HANDLE for subsequent use.

Table 18.1. Exceptions raised by FOPEN.

Error Raised Description of Error
UTL_FILE.INVALID_PATH Directory path or filename specified is invalid, or the file is not accessible.
UTL_FILE.INVALID_MODE Invalid mode specified. Raised if using any other characters except R, W, and A.
UTL_FILE.INVALID_OPERATION Due to lack of permissions for access to the file. Contact the DBA for access rights.
UTL_FILE.INTERNAL_ERROR Error internal to the system.

Using IS_OPEN to Test If a File Is Open
As part of error control, before you attempt to open a file, you can test to see if the file is already open by using IS_OPEN. You can also test to make sure a file is open before you attempt to close the file.

Previous | Table of Contents | Next

Используются технологии uCoz