oracle 如何在文件系统上创建目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1554079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 02:09:54  来源:igfitidea点击:

How can I create a directory on the file system?

oracleplsqloracle10gdirectory

提问by TrojanName

How do you create a physical directory on the OS from within PL/SQL? I looked at the CREATE OR REPLACE DIRECTORYcommand but that doesn't do it. Neither does UTL_FILEappear to be capable.

如何从 PL/SQL 中在 OS 上创建物理目录?我查看了CREATE OR REPLACE DIRECTORY命令,但没有这样做。两者UTL_FILE似乎都没有能力。

回答by dpbradley

UTL_FILEstill lacks this capability - probably a holdover from the pre-DIRECTORYobject days where you had to explicitly define the OS file directories you could access in a startup parameter, so there was no need to create directories dynamically anyway.

UTL_FILE仍然缺乏这种功能 - 可能是从前DIRECTORY对象时代的遗留问题,您必须明确定义您可以在启动参数中访问的操作系统文件目录,因此无论如何都不需要动态创建目录。

I think the easiest way to do this is with an Oracle Java stored procedure that uses:

我认为最简单的方法是使用 Oracle Java 存储过程,它使用:

    File f = new File(dirname);
    return (f.mkdir()) ? 1 : 0;

If you go this route make sure that you use dbms_java.grant_permissionto grant java.io.FilePermissionto the user that owns the executing code.

如果您走这条路线,请确保您使用dbms_java.grant_permission授予java.io.FilePermission拥有执行代码的用户。

回答by TrojanName

In the end I did find an easier solution. Use

最后我确实找到了一个更简单的解决方案。用

select os_command.exec('mkdir /home/oracle/mydir') from dual;

or simply

或者干脆

x := os_command.exec('mkdir /home/oracle/mydir');

回答by RC.

I believe the only way to do this is to use an external procedure (C or Java) and call it through PL/SQL. PL/SQL itself does not have the means to create the physical OS directory.

我相信这样做的唯一方法是使用外部过程(C 或 Java)并通过 PL/SQL 调用它。PL/SQL 本身没有创建物理操作系统目录的方法。

PL/SQL Tipsprovides a good example of how to create a C external procedure that executes shell commands. Note that I would not consider it best practice to allow this for security reasons.

PL/SQL Tips提供了一个很好的例子来说明如何创建一个执行 shell 命令的 C 外部过程。请注意,出于安全原因,我不会认为允许这样做是最佳实践。

It you can create the directory first, then you can use the

你可以先创建目录,然后你可以使用

create or replace directory myDir as '<path-to-dir>/myDir';

create or replace directory myDir as '<path-to-dir>/myDir';

Note that you will need to have the CREATE ANY DIRECTORY privilege assigned to the user executing the command. After the directory is created with the command above, be sure to assign any needed privileges on the directory to other users.

请注意,您需要将 CREATE ANY DIRECTORY 权限分配给执行命令的用户。使用上述命令创建目录后,请确保将目录上所需的任何权限分配给其他用户。

grant read, write on directory myDir to testUsers;

回答by Jim Hudson

I just checked the new docs for database version 11.2, and there's still no routine I can find to create a directory. So, like the other respondents, I recommend using a Java or C routine.

我刚刚检查了数据库版本 11.2 的新文档,但仍然没有找到创建目录的例程。因此,与其他受访者一样,我建议使用 Java 或 C 例程。