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
當我運行這個腳本時,我得到下面的輸出說明目錄被成功創建。但是,當我驗證我的主目錄時,不會創建該目錄,即使該消息確認相同。使用UTL_FILE通過Oracle創建目錄的shell腳本
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() procedure not accepting path for directory?給出的解決方案了,但我仍然不知道如何糾正呢?
這裏的目錄是Oracle的目錄。您可以從dba_directories或all_directories視圖中進行驗證。不要混淆Unix目錄。您需要使用mkdir命令創建unix目錄,然後您需要將文件手動放入此unix目錄以執行ULT_FILE操作。 – San
如果您必須執行讀取操作,則需要放置該文件,但在您需要編寫新文件的情況下,在這種情況下無需放置任何文件,只需手動創建Unix目錄即可。 – San
@San所以你的意思是說不需要'CREATE或者將DIRECTORY LOGDIR AS替換成'〜/ log'; 授予斯科特目錄登錄日誌;'? –