Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to write to a file ?

Posted on 2004-04-19
Medium Priority
Last Modified: 2007-12-19

I have a stupid problem...
I know that I have to use the UTL_FILE built in and I know how to use it.
I know that I have to give access to the folder I want to use with specifying UTL_FILE_DIR in the init.ora file.
My problem is that I do not have any init.ora file anywhere....
I have tried to create a simple one with the new UTL_FILE_DIR sentence but it doesn't seem to work.

To be more precise, my DB is installed on Computer1 and I run my queries from Computer2 from TOAD.
And I want to have some output in a file.
Where do I have to set my Init.ora file ? On Computer1 or Computer2 ? and in which folder ? (I have told... stupid question...)
My generated file can be on Computer1 ?
Can you give me the example of Init.ora file ? I can set only the UTL_FILE_DIR in it ?

btw, I have also read that I could give access with ALTER SYSTEM command while logged as SYSDBA. But it doesn't seem to work, giving me the error 'Cannot change initialization options' or something like that...
Question by:Jarodtweiss
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 23

Assisted Solution

seazodiac earned 150 total points
ID: 10863097
thato's because your database is started with SPFILE instead of PFILE.

try this in Toad or SQLPLUS

SQL> select count(*) from v$spparameter where isspecified ='TRUE';

if you get count(*) >0, then you are using SPFILE,

And if you are lucky (running database version 8i or later),

then you can use this to modify utl_file_dir dynamically

SQL> alter system set ult_file_dir='/foo/bar/dir1' scope=spfile
LVL 10

Expert Comment

ID: 10863355
If you are running a 9i (9.2) database you don't need to set a UTL_FILE_DIR in the init.ora or pfile anymore.

Log in as SYSDBA
SQL> CREATE DIRECTORY my_directory AS 'C:\NEW'; -- this is on the DB server
SQL> GRANT READ ON DIRECTORY my_directory TO scott; -- read is enough to read/write to the location

Now you can connect as SCOTT and use 'my_directory' as the first parameter in UTL_FILE.FOPEN like this
file_handle := utl_file.fopen('my_directory','output.txt','w');

Note : Case of the directory is important here, use it in the same case that it was created.

If you need to write to a remote location (not on the DB server) then the database and listener services need to run under a domain usercode which has access to the remote location.

Expert Comment

ID: 10865706
if you use NT platform, open regedit, read HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME[0, 1, 2 or other ]\ORA_TEST_PFILE, may be your init.ora have another place and file name.
if you don`t find him, run
select NAME, VALUE
    from v$parameter
    where isdefault = 'FALSE'
it`s be your init.ora (NAME=VALUE), put them on normal place.
create file dir, make access to him,
then change parameter UTL_FILE_DIR in new init.ora
Now you can use UTL_FILE

Best regards
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  


Author Comment

ID: 10866916

"select count(*) from v$spparameter where isspecified ='TRUE'" gives me 29
So I run the alter which is accepted.
Then I run teh select again and it gives me 30. If I check in the v$parameter, the utl_file_dir has not been changed
andI still have the same problem then


I'm using Oracle 9.2 but your solution doesn't seem to work
I connect as a SYSDBA (either on my machine or directly on the DB server)
the CREATE DIRECTORY and GRANT were ok (no error) but when I run the utl_file commands, I still have the error (not a vlalid...)
I checked and cheked again the path and case and it was ok.


I'm using W2K
and I have nothing in the registry ang the select show me the path of dumps and control files. That's all

Any other idea ? or precision if your solution should have work. I may have done something wrong...

Expert Comment

ID: 10867084
Exporting the Server Parameter File
You can export a server parameter file to create a traditional text initialization parameter file. Reasons for doing this include:

Creating backups of the server parameter file

For diagnostic purposes, listing all of the parameter values currently used by an instance. This is analogous to the SQL*Plus SHOW PARAMETERS command or selecting from the V$PARAMETER or V$PARAMETER2 views.

Modifying of the server parameter file by first exporting it, editing the output file, and then recreating it.

The exported file can also be used to start up an instance using the PFILE option.

The CREATE PFILE statement is used to export a server parameter file. You must have the SYSDBA or the SYSOPER system privilege to execute this statement. The exported file is created on the database server machine. It contains any comments associated with the parameter in the same line as the parameter setting.

The following example creates a text initialization parameter file from the server parameter file:


Because no names were specified for the files, a platform-specific name is used for the initialization parameter file, and it is created from the platform-specific default server parameter file.

The following example creates a text initialization parameter file from a server parameter file where the names of the files are specified:

CREATE PFILE='/u01/oracle/dbs/test_init.ora'
       FROM SPFILE='/u01/oracle/dbs/test_spfile.ora';

Best regards

Author Comment

ID: 10867093
Finally it's ok.
I was able to create manually a init.ora file and it is loaded correctly in Oracle.
I can now write to the file.
I'm checking if I don't have extra problems before closing the question.

Accepted Solution

GDE earned 225 total points
ID: 10867108
if you don`t find init.ora file create him from v$parameter view, then run create blah-blah-blah...FROM... Stop db, restart using parameter pfile. Of course, change UTL_FILE_DIR parameter.

Best regards

Author Comment

ID: 10867252
Now I have an error when I write to the file.
In all the case, it is ok except for that line :

         UTL_FILE.PUT_LINE(file_id, myQuery);
Notice that myQuery is a VARCHAR2 and that has a length of 20 000 or so...
And I receive a UTL_FILE.WRITE_ERROR exception.
Is it because of the length ? Do I have to substring it ? There is no other way ?

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question