How to write to a file ?


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...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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.
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
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

JarodtweissAuthor Commented:

"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...
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
JarodtweissAuthor Commented:
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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JarodtweissAuthor Commented:
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 ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.