0

我正在尝试查看此文件是否存在。但我收到此错误消息。我已经检查了我获得的特权。但是这个文件在服务器端所以我缺少什么

DECLARE
  vInHandle  utl_file.file_type;

BEGIN

  vInHandle := utl_file.fopen('IMG_UTL_DIR', 'image-file.csv', 'R');

  IF utl_file.is_open(vInHandle) THEN

      dbms_output.put_line('The File exists');

  Else

      dbms_output.put_line('The File not  exists');
  END IF;
END fopen;

错误:

ORA-29283:无效的文件操作
ORA-06512:在“SYS.UTL_FILE”,第 536 行
ORA-29283:无效的文件操作
ORA-06512:在第 5 行

4

1 回答 1

3

如果该文件不存在,那么您将收到该错误。使用您的代码,当文件存在时,您将获得:

anonymous block completed
The File exists

但是当文件不存在时,你会得到:

Error report -
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at line 6
29283. 00000 -  "invalid file operation"
*Cause:    An attempt was made to read from a file or directory that does
           not exist, or file or directory access was denied by the
           operating system.
*Action:   Verify file and directory access privileges on the file system,
           and if reading, verify that the file exists.

请注意错误描述的“不存在的文件或目录”部分。您不能像这样测试文件的存在。据我所知,没有直接的方法来测试那里的文件。您将不得不尝试打开文件并捕获异常。例如:

DECLARE
  vInHandle  utl_file.file_type;
  eNoFile    exception;
  PRAGMA exception_init(eNoFile, -29283);
BEGIN
  BEGIN
    vInHandle := utl_file.fopen('IMG_UTL_DIR', 'image-file.csv', 'R');
    dbms_output.put_line('The File exists');
  EXCEPTION
    WHEN eNoFile THEN
      dbms_output.put_line('The File not  exists');
  END;
END fopen;
/

anonymous block completed
The File not  exists

但正如描述所说,ORA-29283 异常也可能意味着其他事情,所以它并不一定意味着文件不存在 - 它可能存在但由于其他(与权限相关的)原因而无法访问。您还将在某种程度上掩盖实际错误的位置,如果您在块中有多个文件操作,那么您要么必须将每个操作包装在其自己的开始/异常/结束子块中以指定错误,或者丢失实际的错误点。

您最好让异常自然地引发​​和报告,而不是捕获它并用dbms_output可能不会被客户端检索和显示的消息替换它。

于 2014-08-21T07:30:43.187 回答