How to write to a text file from Pl/SQL, PLS error 00363

Bato-Bair Tsyrenov picture Bato-Bair Tsyrenov · May 30, 2014 · Viewed 56.4k times · Source

I am trying to write to a file from a procedure:

out_File := Utl_File.FOpen('C:\test', 'batotest.txt' , 'W');

  Utl_File.Put_Line(out_file , 'Hi this is text file!');
  Utl_File.FClose(out_file);

Compilation errors for PACKAGE xxxxxxxx

Error: PLS-00363: âûðàæåíèå 'OUT_FILE' íå ì.á. èñïîëüçîâàíî êàê àäðåñàò íàçíà÷åíèÿ
Line: 795
Text: out_File := Utl_File.FOpen('C:\test', 'batotest.txt' , 'W');

Error: PL/SQL: Statement ignored
Line: 795
Text: out_File := Utl_File.FOpen('C:\test', 'batotest.txt' , 'W');

Error: PLS-00363: 'OUT_FILE' íå ì.á. èñïîëüçîâàíî êàê àäðåñàò íàçíà÷åíèÿ
Line: 797
Text: Utl_File.FClose(out_file);

Error: PL/SQL: Statement ignored
Line: 797
Text: Utl_File.FClose(out_file);

So this is my code and it gives me this error, what is wrong?

Answer

First, you need to create a directory object to access the C:\test directory:

CREATE OR REPLACE DIRECTORY CTEST AS 'C:\test';
GRANT READ ON DIRECTORY CTEST TO PUBLIC; 

Next, you need to use this directory object when opening your file:

DECLARE
  out_File  UTL_FILE.FILE_TYPE;
BEGIN
  out_File := UTL_FILE.FOPEN('CTEST', 'batotest.txt' , 'W');

  UTL_FILE.PUT_LINE(out_file , 'Hi this is text file!');
  UTL_FILE.FCLOSE(out_file);
END;

Share and enjoy.