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

发布于 2024-08-08 07:58:08 字数 120 浏览 1 评论 0原文

如何从 PL/SQL 中在操作系统上创建物理目录?我查看了 CREATE OR REPLACE DIRECTORY 命令,但这并不起作用。 UTL_FILE 似乎也没有能力。

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

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

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

发布评论

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

评论(5

想挽留 2024-08-15 07:58:08

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

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

或简单地

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

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');
贪了杯 2024-08-15 07:58:08

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

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

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

如果您选择此路线,请确保使用 dbms_java.grant_permission 授予 java.io.FilePermission code> 拥有执行代码的用户。

UTL_FILE still lacks this capability - probably a holdover from the pre-DIRECTORY object 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.

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

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

If you go this route make sure that you use dbms_java.grant_permission to grant java.io.FilePermission to the user that owns the executing code.

酒中人 2024-08-15 07:58:08

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

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

如果您可以先创建目录,那么您可以使用

创建或替换目录 myDir 作为 '/myDir';

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

grant read, write on directory myDir to testUsers;

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.

PL/SQL Tips provides 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.

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

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.

grant read, write on directory myDir to testUsers;
﹂绝世的画 2024-08-15 07:58:08

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

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.

不必了 2024-08-15 07:58:08

您可以使用 DBMS_SCHEDULER 或内部 Java 过程从 Oracle 中执行操作系统命令,例如,使用我的 XT_SHELL 软件包:

  1. 使用 install.sql 安装它:
    输入图片此处描述

  2. 在 SQL 或 PL/SQL 中使用 xt_shell.shell_exec(pCommand in varchar2,timeout in number) 执行操作系统命令:

SQL> select * from table(xt_shell.shell_exec('/bin/mkdir /tmp/test-dir',1000));

COLUMN_VALUE
--------------------------------------------------------------------------------


SQL> select * from table(xt_shell.shell_exec('/bin/mkdir /tmp/test-dir/test-dir2',1000));

COLUMN_VALUE
--------------------------------------------------------------------------------


SQL> select * from table(xt_shell.shell_exec('/bin/ls -l /tmp/test-dir',1000));

COLUMN_VALUE
--------------------------------------------------------------------------------
total 4
drwxr-xr-x 2 oracle oinstall 4096 Apr 19 12:14 test-dir2

You can execute OS commands from within Oracle using DBMS_SCHEDULER or internal Java procedure, for example, using my XT_SHELL package:

  1. install it using install.sql:
    enter image description here

  2. Execute OS command using xt_shell.shell_exec(pCommand in varchar2,timeout in number) in SQL or PL/SQL:

SQL> select * from table(xt_shell.shell_exec('/bin/mkdir /tmp/test-dir',1000));

COLUMN_VALUE
--------------------------------------------------------------------------------


SQL> select * from table(xt_shell.shell_exec('/bin/mkdir /tmp/test-dir/test-dir2',1000));

COLUMN_VALUE
--------------------------------------------------------------------------------


SQL> select * from table(xt_shell.shell_exec('/bin/ls -l /tmp/test-dir',1000));

COLUMN_VALUE
--------------------------------------------------------------------------------
total 4
drwxr-xr-x 2 oracle oinstall 4096 Apr 19 12:14 test-dir2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文