UTL_FILE.FOPEN() 过程不接受目录路径?

发布于 2024-08-30 15:22:39 字数 1231 浏览 5 评论 0原文

我正在尝试写入存储在 c:\ 驱动器中名为 vin1.txt 的文件 并收到此错误。请提出建议!

> ERROR at line 1: ORA-29280: invalid
> directory path ORA-06512: at
> "SYS.UTL_FILE", line 18 ORA-06512: at
> "SYS.UTL_FILE", line 424 ORA-06512: at
> "SCOTT.SAL_STATUS", line 12 ORA-06512:
> at line 1

这是代码

  create or replace procedure sal_status
   (
    p_file_dir IN varchar2,
    p_filename IN varchar2)
     IS  
    v_filehandle utl_file.file_type;
    cursor emp Is
        select * from employees
        order by department_id;
    v_dep_no departments.department_id%TYPE;
     begin
         v_filehandle :=utl_file.fopen(p_file_dir,p_filename,'w');--Opening a file
         utl_file.putf(v_filehandle,'SALARY REPORT :GENERATED ON %s\n',SYSDATE);
         utl_file.new_line(v_filehandle);
         for v_emp_rec IN emp LOOP
            v_dep_no :=v_emp_rec.department_id;
            utl_file.putf(v_filehandle,'employee %s earns:s\n',v_emp_rec.last_name,v_emp_rec.salary);                    
         end loop;
        utl_file.put_line(v_filehandle,'***END OF REPORT***');
        UTL_FILE.fclose(v_filehandle);
     end sal_status;

execute sal_status('C:\','vin1.txt');--Executing

I am trying to write in a file stored in c:\ drive named vin1.txt
and getting this error .Please suggest!

> ERROR at line 1: ORA-29280: invalid
> directory path ORA-06512: at
> "SYS.UTL_FILE", line 18 ORA-06512: at
> "SYS.UTL_FILE", line 424 ORA-06512: at
> "SCOTT.SAL_STATUS", line 12 ORA-06512:
> at line 1

HERE is the code

  create or replace procedure sal_status
   (
    p_file_dir IN varchar2,
    p_filename IN varchar2)
     IS  
    v_filehandle utl_file.file_type;
    cursor emp Is
        select * from employees
        order by department_id;
    v_dep_no departments.department_id%TYPE;
     begin
         v_filehandle :=utl_file.fopen(p_file_dir,p_filename,'w');--Opening a file
         utl_file.putf(v_filehandle,'SALARY REPORT :GENERATED ON %s\n',SYSDATE);
         utl_file.new_line(v_filehandle);
         for v_emp_rec IN emp LOOP
            v_dep_no :=v_emp_rec.department_id;
            utl_file.putf(v_filehandle,'employee %s earns:s\n',v_emp_rec.last_name,v_emp_rec.salary);                    
         end loop;
        utl_file.put_line(v_filehandle,'***END OF REPORT***');
        UTL_FILE.fclose(v_filehandle);
     end sal_status;

execute sal_status('C:\','vin1.txt');--Executing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

亚希 2024-09-06 15:22:39

从 Oracle 9i 开始,有两种方法可以声明与 UTL_FILE 一起使用的目录。

较旧的方法是设置 INIT.ORA 参数 UTL_FILE_DIR。我们必须重新启动数据库才能使更改生效。该值可以像任何其他 PATH 变量一样;它接受通配符。使用这种方法意味着传递目录路径...

UTL_FILE.FOPEN('c:\temp', 'vineet.txt', 'W');

9i 中引入的替代方法是声明一个目录对象。

create or replace directory temp_dir as 'C:\temp'
/

grant read, write on directory temp_dir to vineet
/

目录对象需要确切的文件路径,并且不接受通配符。在这种方法中,我们传递目录对象名称...

UTL_FILE.FOPEN('TEMP_DIR', 'vineet.txt', 'W');

UTL_FILE_DIR 已被弃用,因为它本质上是不安全的 - 所有用户都可以访问路径中指定的所有操作系统目录,而目录对象的读写权限可以单独授予个人用户。此外,使用 Directory 对象,我们可以添加、删除或更改目录,而无需弹回数据库。

无论哪种情况,oracle 操作系统用户都必须对操作系统目录具有读取和/或写入权限。如果不明显,这意味着该目录必须对数据库服务器可见。因此,我们不能使用任何一种方法将本地 PC 上的目录公开给远程数据库服务器上运行的进程。文件必须上传到数据库服务器或共享网络驱动器。


如果oracle操作系统用户对操作系统目录没有适当的权限,或者数据库中指定的路径与实际路径不匹配,则程序将抛出此异常:

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

The OERR text for这个错误非常明显:

29283 -  "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.

从 Oracle 18c 开始,除了向后兼容性之外,不支持 UTL_FILE_DIR。

Since Oracle 9i there are two ways or declaring a directory for use with UTL_FILE.

The older way is to set the INIT.ORA parameter UTL_FILE_DIR. We have to restart the database for a change to take affect. The value can like any other PATH variable; it accepts wildcards. Using this approach means passing the directory path...

UTL_FILE.FOPEN('c:\temp', 'vineet.txt', 'W');

The alternative approach introduced in 9i is to declare a directory object.

create or replace directory temp_dir as 'C:\temp'
/

grant read, write on directory temp_dir to vineet
/

Directory objects require the exact file path, and don't accept wildcards. In this approach we pass the directory object name...

UTL_FILE.FOPEN('TEMP_DIR', 'vineet.txt', 'W');

The UTL_FILE_DIR is deprecated because it is inherently insecure - all users have access to all the OS directories specified in the path, whereas read and write privileges on Directory objects can be granted discretely to individual users. Also, with Directory objects we can be add, remove or change directories without bouncing the database.

In either case, the oracle OS user must have read and/or write privileges on the OS directory. In case it isn't obvious, this means the directory must be visible from the database server. So we cannot use either approach to expose a directory on our local PC to a process running on a remote database server. Files must be uploaded to the database server, or a shared network drive.


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
ORA-06512: at line 7

The OERR text for this error is pretty clear:

29283 -  "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.

As of Oracle 18c UTL_FILE_DIR is not supported, except for backwards compatibility.

樱&纷飞 2024-09-06 15:22:39

还不要忘记该文件的路径位于实际的 Oracle 服务器计算机上,而不是可能调用存储过程的任何本地开发计算机上。这可能是非常明显但应该记住的事情。

Don't forget also that the path for the file is on the actual oracle server machine and not any local development machine that might be calling your stored procedure. This is probably very obvious but something that should be remembered.

薄荷港 2024-09-06 15:22:39

您需要向 Oracle 注册该目录。 fopen 采用目录对象的名称,而不是路径。例如:(

您可能需要以SYS身份登录才能执行这些)

CREATE DIRECTORY MY_DIR AS 'C:\';

GRANT READ ON DIRECTORY MY_DIR TO SCOTT;

然后,您可以在调用fopen时引用它:

execute sal_status('MY_DIR','vin1.txt');

You need to register the directory with Oracle. fopen takes the name of a directory object, not the path. For example:

(you may need to login as SYS to execute these)

CREATE DIRECTORY MY_DIR AS 'C:\';

GRANT READ ON DIRECTORY MY_DIR TO SCOTT;

Then, you can refer to it in the call to fopen:

execute sal_status('MY_DIR','vin1.txt');
第七度阳光i 2024-09-06 15:22:39

目录名称似乎区分大小写。我遇到了同样的问题,但是当我提供大写的目录名称时,它起作用了。

The directory name seems to be case sensitive. I faced the same issue but when I provided the directory name in upper case it worked.

再可℃爱ぅ一点好了 2024-09-06 15:22:39

您需要让 DBA 修改 init.ora 文件,将要访问的目录添加到“utl_file_dir”参数中。然后,您的数据库实例将需要停止并重新启动,因为只有在启动数据库时才会读取 init.ora。

您可以通过运行以下查询来查看(但不能更改)此参数:

SELECT *
  FROM V$PARAMETER
  WHERE NAME = 'utl_file_dir'

共享并享受。

You need to have your DBA modify the init.ora file, adding the directory you want to access to the 'utl_file_dir' parameter. Your database instance will then need to be stopped and restarted because init.ora is only read when the database is brought up.

You can view (but not change) this parameter by running the following query:

SELECT *
  FROM V$PARAMETER
  WHERE NAME = 'utl_file_dir'

Share and enjoy.

悲凉≈ 2024-09-06 15:22:39

对于 utl_file.open(location,filename,mode) ,我们需要为位置提供目录名称,而不是路径。例如:DATA_FILE_DIR ,这是目录名称,并检查该特定目录名称的目录路径。

For utl_file.open(location,filename,mode) , we need to give directory name for location but not path. For Example:DATA_FILE_DIR , this is the directory name and check out the directory path for that particular directory name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文