0
#! /bin/bash

sqlplus -s /nolog  << EOF
conn sys/password as sysdba
CREATE or replace DIRECTORY LOGDIR AS '~/log';
GRANT WRITE ON DIRECTORY LOGDIR TO scott; 

connect scott/tiger
set serveroutput on feedback off
CREATE OR REPLACE PROCEDURE SP_ORACLE_2_EXCEL(filename IN VARCHAR2) AS
    filehandle UTL_FILE.FILE_TYPE;
    filepath VARCHAR2(50);
    CURSOR C1 IS SELECT * FROM emp order by deptno;
    VARC1 C1%ROWTYPE;
BEGIN
    filepath := filename || '_' || SYSDATE || '.csv';    
    filehandle := UTL_FILE.FOPEN('LOGDIR', filepath, 'W');
    utl_file.putf(filehandle,' REPORT :GENERATED ON %s\n',SYSDATE);
    utl_file.new_line(filehandle);
    UTL_FILE.PUT_LINE(filehandle, 'EMPNO' || ',' || 'ENAME' || ',' || 'DEPTNO');
    OPEN C1;
    LOOP
        FETCH C1 INTO VARC1;
        EXIT WHEN C1%NOTFOUND;
        UTL_FILE.PUT_LINE(filehandle, '"' || VARC1.empno || '"' || ' ,' || '"' ||
            VARC1.ename || '"' || ' ,' || '"' || VARC1.deptno|| '"');
    END LOOP;
    close C1;
    UTL_FILE.FFLUSH(filehandle);
    UTL_FILE.FCLOSE(filehandle);
EXCEPTION
    WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLCODE||','||SQLERRM);
END SP_ORACLE_2_EXCEL;
/
execute SP_ORACLE_2_EXCEL('test')
quit
EOF

当我运行此脚本时,我得到以下输出,说明目录已成功创建。但是,当我验证我的主目录时,不会创建该目录,即使消息确认相同。

Directory created.

Grant succeeded.

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

我已经完成了UTL_FILE.FOPEN() 过程中给出的解决方案,不接受目录路径?,但我仍然不确定如何纠正这个问题?

4

1 回答 1

0

不像那样,Oracle 目录 LOGDIR 是给 '~/log' 文件夹的名称。现在,您的操作系统通过名称“~/log”识别此日志文件夹,但 Oracle 通过 LOGDIR 名称识别同一文件夹。因此,在您的主目录中创建一个文件夹 log,然后告诉 oracle 您可以通过使用 CREATE 创建一个 Oracle 目录来将 ~/log 位置标识为 LOGDIR,或者将 DIRECTORY LOGDIR 替换为“~/log”。两者都是必需的。– 桑

于 2014-11-24T12:54:48.750 回答