invalid path while trying to write the file in pl sql

霸气de小男生 提交于 2020-01-24 19:54:06

问题


i am trying to write to a text file in oracle pl sql(10 g). i have created the directory it is existing but still getting invalid path. below is the code.

CREATE DIRECTORY test_dir AS 'c:\';
-- CREATE DIRECTORY test_dir AS '/tmp';

DECLARE
  fileHandler UTL_FILE.FILE_TYPE;
BEGIN
  fileHandler := UTL_FILE.FOPEN('test_dir', 'test_file.txt', 'W');
  UTL_FILE.PUTF(fileHandler, 'Writing TO a file\n');
  UTL_FILE.FCLOSE(fileHandler);
EXCEPTION
  WHEN utl_file.invalid_path THEN
     raise_application_error(-20000, 'ERROR: Invalid PATH FOR file.');
END;
/

the following is the error:
*
ERROR at line 1:
ORA-20000: ERROR: Invalid PATH FOR file.
ORA-06512: at line 9

回答1:


Oracle is case sensitive. But all names in SQL and PL/SQL are automatically converted to uppercase unless enclosed in double quotes.

So:

CREATE DIRECTORY test_dir AS 'c:\';

is actually executed as:

CREATE DIRECTORY TEST_DIR AS 'c:\';

Therefore the directory you have defined is called TEST_DIR. If you refer to it in a string (as opposed to a symbol name in SQL or PL/SQL), you must use 'TEST_DIR'. 'test_dir' won't work.

Thus try:

fileHandler := UTL_FILE.FOPEN('TEST_DIR', 'test_file.txt', 'W');


来源:https://stackoverflow.com/questions/40873133/invalid-path-while-trying-to-write-the-file-in-pl-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!