#! /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() 过程中给出的解决方案,不接受目录路径?,但我仍然不确定如何纠正这个问题?