无法使用外部表读取文件或使用 PL/SQL 使用 UTL_FILE 写入文件

问题描述 投票:0回答:2

我遇到一个问题,无法使用外部表读取简单文件,也无法使用

UTL_FILE
写入文件。我认为这与权限有关,但我无法弄清楚。

我确认

APPS
PUBLIC
有足够的权限:

select GRANTEE, privilege from all_tab_privs
where  table_name = 'EXT_TAB_DATA';

GRANTEE PRIVILEGE
------- --------
APPS    WRITE
APPS    READ
APPS    EXECUTE
PUBLIC  WRITE
PUBLIC  READ
PUBLIC  EXECUTE

我还确认了实际的目录已定义:

select * from all_directories
where  directory_name = 'EXT_TAB_DATA';

OWNER   DIRECTORY_NAME  DIRECTORY_PATH              ORIGIN_CON_ID
------  --------------  --------------------------  -----------------
SYS     EXT_TAB_DATA    /u01/app/oracle/DEV/SAMPLE  0

以下是目录的权限

/u01/app/oracle/DEV/SAMPLE

[appldev-run ~]$ ls -l /u01/app/oracle/DEV
total 24
-rw-r--r-- 1 appldev appldev 6473 Jun 19 15:10 EBSapps.env
drwxr-xr-x 5 appldev appldev 4096 Jun  4 10:13 fs1
drwxr-xr-x 5 appldev appldev 4096 Jun  7 16:26 fs2
drwxr-xr-x 4 appldev appldev 4096 May 22 12:32 fs_ne
-rw------- 1 root    root       0 Sep 18  2018 nohup.out
drwxrwxrwx 2 oracle  appldev 4096 Jun 25 02:31 SAMPLE

当我尝试在下面编写一个简单的

UTL_FILE
命令时:

declare 
  fHandle  UTL_FILE.FILE_TYPE;
begin
  fHandle := UTL_FILE.FOPEN('EXT_TAB_DATA', 'test_file', 'w');

  UTL_FILE.PUT(fHandle, 'This is the first line');
  UTL_FILE.PUT(fHandle, 'This is the second line');
  UTL_FILE.PUT_LINE(fHandle, 'This is the third line');

  UTL_FILE.FCLOSE(fHandle);
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Exception: SQLCODE=' || SQLCODE || '  SQLERRM=' || SQLERRM);
    RAISE;
end;
/

它会导致如下错误:

ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at line 14
29283. 00000 -  "invalid file operation"
*Cause:    An attempt was made to read from a file or directory that does
           not exist, or file or directory access was denied by the
           operating system.
*Action:   Verify file and directory access privileges on the file system,
           and if reading, verify that the file exists.

我什至尝试从外部表中读取:

CREATE TABLE sample_ext
( sample1 varchar(10) )
organization external (
  default directory EXT_TAB_DATA
  location ('test.txt')
  );
/

select  *
from    sample_ext;  
/

它会导致以下错误:

ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-04001: error opening file /u01/app/oracle/DEV/SAMPLE/SAMPLE_EXT_62883.log
29913. 00000 -  "error in executing %s callout"
*Cause:    The execution of the specified callout caused an error.
*Action:   Examine the error messages take appropriate action.

我确认该文件存在并且包含数据:

[appldev-run@hamlfinappdev ~]$ cd /u01/app/oracle/DEV/SAMPLE
[appldev-run@hamlfinappdev ~]$ cat test.txt
1
1
1
1
[appldev-run@hamlfinappdev ~]$

我需要做什么才能读取和写入该目录?

数据库版本如下:

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
PL/SQL Release 12.1.0.2.0 - Production
CORE    12.1.0.2.0      Production
TNS for Linux: Version 12.1.0.2.0 - Production
NLSRTL Version 12.1.0.2.0 - Production
linux database oracle unix plsql
2个回答
1
投票

您需要授予该目录的所有权限

chmod 755 DEV
或更改该目录的所有者
chown oracle:appdev DEV
。 我重复了这种情况并得到了同样的错误。 我创建了一个测试环境。

用户根

esmd:/opt/oracle # ls -l |grep DEV
drwxrwx--- 3 appdev appdev    4096 2019-06-25 07:17 DEV
esmd:/opt/oracle/DEV # ls -l
total 4
drwxrwxrwx 2 oracle dba 4096 2019-06-25 07:22 SAMPLE

esmd:/opt/oracle # cd DEV/SAMPLE/
esmd:/opt/oracle/DEV/SAMPLE # ls -l

total 4
-rw-r--r-- 1 oracle dba 8 2019-06-25 07:14 test.txt

用户预言机

oracle@esmd:~> more /opt/oracle/DEV/SAMPLE/test.txt
/opt/oracle/DEV/SAMPLE/test.txt: Permission denied

用户应用程序开发

oracle@esmd:~> su appdev
Password:
appdev@esmd:/opt/oracle> ls -l /opt/oracle/DEV/SAMPLE/test.txt
-rw-r--r-- 1 oracle dba 8 2019-06-25 07:14 /opt/oracle/DEV/SAMPLE/test.txt

数据库用户系统

CREATE OR REPLACE DIRECTORY EXT_TAB_DATA AS '/opt/oracle/DEV/SAMPLE';

CREATE TABLE sample_ext
( sample1 varchar(10) )
organization external (
  default directory EXT_TAB_DATA
  location ('test.txt')
  );
select  *
from    sample_ext;  


07:34:14  line 1: ORA-29913: error in executing ODCIEXTTABLEOPEN callout
07:34:14  ORA-29400: data cartridge error
07:34:14  error opening file /opt/oracle/DEV/SAMPLE/SAMPLE_EXT_4977.log

我更改了 DEV 目录

chmod 757 DEV
,一切正常!

appdev@esmd:/opt/oracle> ls -l /opt/oracle |grep DEV
drwxrwx--- 3 appdev appdev   4096 2019-06-25 07:17 DEV
appdev@esmd:/opt/oracle> chmod 775 DEV
appdev@esmd:/opt/oracle> ls -l /opt/oracle |grep DEV
drwxrwxr-x 3 appdev appdev   4096 2019-06-25 07:17 DEV
select  *
from    sample_ext; 
SAMPLE1                                
---------------- 
1                
1                
1                
1                

我更改了 DEV 目录

chmod 775 DEV or  chown oracle:appdev DEV
,一切正常!

   oracle@esmd:~> su
Password:

esmd:/opt/oracle # ls -l |grep DEV
drwxrwxr-x 3 appdev appdev    4096 2019-06-25 07:17 DEV
esmd:/opt/oracle # chown oracle:appdev DEV
esmd:/opt/oracle # ls -l |grep DEV
drwxrwxr-x 3 oracle appdev    4096 2019-06-25 07:17 DEV
esmd:/opt/oracle # chmod 770 DEV
esmd:/opt/oracle # ls -l |grep DEV
drwxrwx--- 3 oracle appdev    4096 2019-06-25 07:17 DEV
esmd:/opt/oracle #



select  *
    from    sample_ext; 
    SAMPLE1                                
    ---------------- 
    1                
    1                
    1                
    1    

0
投票

对于 Windows 上的 Oracle:(归功于 Chris Saxon...)

  • 检查oracle安装目录(ORACLE_HOME) 属性(上下文菜单) 检查安全选项卡 组或用户名:ORA_***_SVCACCTS

  • 将 ORA_***_SVCACCTS 添加到您的 oracle 目录

  • 使用 utl_file 进行单元测试(有效!!!)

© www.soinside.com 2019 - 2024. All rights reserved.