Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

PLSQL : Open file in write mode

It is probably something simple but I ask it anyway :

I want to popen a file in write mode (not appending), it starts from the beginning. If the file does not exist, it should be created it.

I tried this :

ficErrors := UTL_FILE.FOPEN(myDirectory, errorsFilename, 'W');

I have this error :

ORA-29283 invalid file operation ORA-06512 at "SYS.UTL_FILE"

ty

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do it like:

Declare

  fHandle  UTL_FILE.FILE_TYPE;
begin
  fHandle := UTL_FILE.FOPEN('BDUMP', 'test_file', 'w');

  UTL_FILE.PUT_Line(fHandle, 'This is the first line');
  UTL_FILE.PUT_Line(fHandle, 'This is the second line');
  UTL_FILE.PUT_LINE(fHandle, 'This is the third line');

  UTL_FILE.FCLOSE(fHandle);
END;

where BDUMP = is the directory where my file will be created. It's on the server where Oracle is installed.

See Demo:

SQL> Declare
        fHandle  UTL_FILE.FILE_TYPE;
      begin
          fHandle := UTL_FILE.FOPEN('BDUMP', 'test_file', 'w');

          UTL_FILE.PUT_Line(fHandle, 'This is the first line');
          UTL_FILE.PUT_Line(fHandle, 'This is the second line');
          UTL_FILE.PUT_LINE(fHandle, 'This is the third line');

          UTL_FILE.FCLOSE(fHandle);
      END;  
  /

PL/SQL procedure successfully completed.

Output:

unixterminal$ ls -lrt test_file
-rw-r--r--   1 oracle   dba           70 Mar 10 15:21 test_file

Note:

If the oracle OS user does not have the appropriate privileges on the OS directory, or if the path specified in the database does not match to an actual path, the program will hurl this exception:

ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...