• Using Create directory & UTL_FILE in Oracle


    作者:eygle  版权声明:转载时请务必以超链接形式标明文章和作者信息及本声明
    链接:

    Create directory让我们可以在Oracle数据库中灵活的对文件进行读写操作,极大的提高了Oracle的易用性和可扩展性。
    其语法为:
    CREATE [OR REPLACE] DIRECTORY directory AS 'pathname';

    本案例具体创建如下:

    create or replace directory exp_dir as '/tmp';

    目录创建以后,就可以把读写权限授予特定用户,具体语法如下:
    GRANT READ[,WRITE] ON DIRECTORY directory TO username;

    例如:

    grant read, write on directory exp_dir to eygle;

    此时用户eygle就拥有了对该目录的读写权限。

    让我们看一个简单的测试:

    SQL> create or replace directory UTL_FILE_DIR as '/opt/oracle/utl_file'; Directory created. SQL> declare 2 fhandle utl_file.file_type; 3 begin 4 fhandle := utl_file.fopen('UTL_FILE_DIR', 'example.txt', 'w'); 5 utl_file.put_line(fhandle , 'eygle test write one'); 6 utl_file.put_line(fhandle , 'eygle test write two'); 7 utl_file.fclose(fhandle); 8 end; 9 / PL/SQL procedure successfully completed. SQL> ! [oracle@jumper 9.2.0]$ more /opt/oracle/utl_file/example.txt eygle test write one eygle test write two [oracle@jumper 9.2.0]$

    类似的我们可以通过utl_file来读取文件:

    SQL> declare 2 fhandle utl_file.file_type; 3 fp_buffer varchar2(4000); 4 begin 5 fhandle := utl_file.fopen ('UTL_FILE_DIR','example.txt', 'R'); 6 7 utl_file.get_line (fhandle , fp_buffer ); 8 dbms_output.put_line(fp_buffer ); 9 utl_file.get_line (fhandle , fp_buffer ); 10 dbms_output.put_line(fp_buffer ); 11 utl_file.fclose(fhandle); 12 end; 13 / eygle test write one eygle test write two PL/SQL procedure successfully completed.

    可以查询dba_directories查看所有directory.

    SQL> select * from dba_directories; OWNER DIRECTORY_NAME DIRECTORY_PATH ------------------------------ ------------------------------ ------------------------------ SYS UTL_FILE_DIR /opt/oracle/utl_file SYS BDUMP_DIR /opt/oracle/admin/conner/bdump SYS EXP_DIR /opt/oracle/utl_file

    可以使用drop directory删除这些路径.

    SQL> drop directory exp_dir; Directory dropped SQL> select * from dba_directories; OWNER DIRECTORY_NAME DIRECTORY_PATH ------------------------------ ------------------------------ ------------------------------ SYS UTL_FILE_DIR /opt/oracle/utl_file SYS BDUMP_DIR /opt/oracle/admin/conner/bdump

  • 相关阅读:
    pyqt5 动画在QThread线程中无法运行问题
    几种编码格式
    Android精品课程—PullToRefresh 下拉刷新
    【张泽华】android视频教程下载地址及上课源代码
    Android中常用适配器及定义自己的适配器
    VirtualBOX 虚拟机安装 OS X 10.9 Mavericks 及 Xcode 5,本人X220亲测
    google maps api申请的问题
    如何在Android应用程序中使用传感器(OpenIntents开源组织SensorSimulator项目)
    cvsnt 设置用户、修改密码
    Windows下用Git下载android源码 转载
  • 原文地址:https://www.cnblogs.com/jimeper/p/910483.html
Copyright © 2020-2023  润新知