UTL_FILE.FOPEN() procedure not accepting path for directory in oracle 12c database? - oracle

Below code is working perfectly in oracle 10g database
DECLARE
g_file_handle UTL_FILE.FILE_TYPE;
g_dir varchar2(100);
BEGIN
g_dir := Scamp_Utils.getutlfiledir;
DBMS_OUTPUT.PUT_LINE (g_dir);
g_file_handle := UTL_FILE.FOPEN (g_dir, 'qwe.txt', 'W', 32767);
UTL_FILE.PUT_LINE (g_file_handle, 'BEGINDATA');
UTL_FILE.FCLOSE (g_file_handle);
COMMIT;
END;
/
Here Scamp_Utils.getutlfiledir is a function which returns the path defined in utl_file_dir.
But same code doesn't work in oracle 12c database. I'm getting an error:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at line 7
I have already provided rwx permission to all at the OS level. I'm still getting this error. I can use the oracle logical directory but I don't want to change code. What should I do here?

Oracle has two option to declare a directory for utl_file.
1. Change `utl_file_dir='/appl/flstadm/utl_files/wlsbb01'` in INIT.ORA file and restart DB. This solution is not recommended and deprecated.
2. Create directory object in DB.
CREATE OR REPLACE DIRECTORY my_dir AS '/appl/flstadm/utl_files/wlsbb01';
GRANT read, write ON DIRECTORY my_dir TO your_user;
g_file_handle := UTL_FILE.FOPEN ('my_dir', 'qwe.txt', 'W', 32767);
Have you added your path to init.ora?

Related

ORA-22288 when trying to read BLOB from Windows directory

I am trying to read BLOB from Windows directory located on the Oracle 11g DB server:
create or replace directory BLOB_DIR as 'C:\data\images';
grant read, write on directory BLOB_DIR to public;
set serveroutput on;
declare
l_bfile BFILE;
begin
l_bfile := BFILENAME('BLOB_DIR', 'blob.jpg');
DBMS_LOB.fileopen(l_bfile, DBMS_LOB.file_readonly);
DBMS_LOB.fileclose(l_bfile);
end;
/
However, I am getting the following error:
ORA-22288: file or LOB operation FILEOPEN failed
ORA-06512: at "SYS.DBMS_LOB", line 744
ORA-06512: at line 5
I checked the directory and file exist and all the permissions are given to Everyone.
What am I doing wrong? How to get more specific error message?

Extracting output from PLSQL procedure to local drive of my laptop

I have a database connection server "server_dev" in sqldeveloper .
Now i want to create a procedure whose output can be directly saved in a csv file for data comparison later in the local drive of my laptop.
So i tried using UTL_FILE oracle package but when i ran the procedure the UTL_FILE was trying to write in the file of the server "server_dev" whereas i dont have any access to that server hence that command isnt working.
for example: the code is:-
CREATE OR REPLACE PROCEDURE export_to_csv_test
IS
v_file UTL_FILE.file_type;
v_string VARCHAR2 (4000);
CURSOR c_contexts
IS
SELECT workspace_id,context_id from contexts where rownum<5;
BEGIN
v_file :=
UTL_FILE.fopen ('Z:\My_Project_knowledge\CSVDIR', 'empdata.csv','w',1000);
FOR cur IN c_contexts
`enter code here`LOOP
v_string :=
cur.workspace_id
|| ','
|| cur.context_id;
UTL_FILE.put_line (v_file, v_string);
END LOOP;
UTL_FILE.fclose (v_file);
END;
for calling it :-
BEGIN
export_to_csv_test;
END;
Error report:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "RAY_DEV07_OWNER.EXPORT_TO_CSV_TEST", line 20
ORA-06512: at line 3
29280. 00000 - "invalid directory path"
*Cause: A corresponding directory object does not exist.
*Action: Correct the directory object parameter, or create a corresponding
directory object with the CREATE DIRECTORY command.
So,I analysed it and found that my SQL developer is connected to a server to my local machin and since its my office laptop I cant alter it.
Can i have any other way in which I can save the output of my stored procedure to my local drive in a text or Csv file?
To write a file to your local machine you may use dbms_output; for example in SQLPlus:
SQL> set feedback off
SQL> set echo off
SQL> set serveroutput on
SQL> spool d:\spool.txt
SQL> begin
2 for i in (select level from dual connect by level <= 5) loop
3 dbms_output.put_line('Level ' || i.level);
4 end loop;
5 end;
6 /
WIll produce the file d:\spool.txt:
Level 1
Level 2
Level 3
Level 4
Level 5
If you can select directly from a table or table function, then SQL*Plus 12.2's new SET MARKUP CSV option will be useful. Instead of paginating the query output it will produce CSV. The full syntax is
SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]
Output generation will faster if you turn on this mode with the sqlplus -m option.
It's also useful for querying JSON types. See https://blogs.oracle.com/opal/entry/fast_generation_of_csv_and

ORA-29283: invalid file operation when reading a file

I have following privileges CREATE ANY DIRECTORY, read, write on directory DOCS to user_name
I am using oracle 10g on Windows.
I have first created the directory as
CREATE DIRECTORY DOCS AS 'C:\Documents and Settings\Owner\Desktop\file';
Directory created successfully.
Now when I tried to execute following code
DECLARE
l_file UTL_FILE.file_type;
l_location VARCHAR2(100) := 'DOCS';
l_filename VARCHAR2(100) := 'test.pdf';
l_text VARCHAR2(32767);
BEGIN
-- Open file.
l_file := UTL_FILE.fopen(l_location, l_filename, 'r', 32767);
-- Read and output first line.
UTL_FILE.get_line(l_file, l_text, 32767);
dbms_output.put_line('First Line: |' || l_text || '|');
-- Close the file.
UTL_FILE.fclose(l_file);
END;
I get these errors
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 475
ORA-29283: invalid file operation
ORA-06512: at line 8
i don't have access to oracle on windows, but something like this in you init.ora file
UTL_FILE_DIR=c:\DOCS
you will then need to restart oracle
you should also be able to do the following (but i've not tested it)
alter system set utl_file_dir=c:\DOCS scope=both

ORACLE 11G UTL_FILE + UTL_FILE_DIR - Invalid Directory

Hi I am running Oracle 11 and I am trying to write to a directory on the server box using UTL_FILE.FOPEN.
To test this I am using the following script (output included):
SQL> #D:\test.sql
declare
l_file_handle UTL_FILE.FILE_TYPE;
begin
l_file_handle := UTL_FILE.FOPEN('/appl/mydir',
'/appl/mydir/filename',
'W');
UTL_FILE.FCLOSE(l_file_handle);
DBMS_OUTPUT.PUT_LINE('FINISHED');
end;
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at line 7
SQL>
I have the /appl/mydir added to the UTL_FILE parameter:
SELECT value
FROM V$PARAMETER
WHERE NAME = 'utl_file_dir';
/appl/mydir, /appl/mydir2, /appl/mydir3
The UNIX directory is writable by all on UNIX:
$ ls -l /appl/mydir
total 0
drwxrwsrwx 2 user userc 363968 Nov 27 13:46 mydir
Using oracle Directory object is not an option and I am aware of the disadvantages of using the UTL_FILE_DIR implementation.
Any ideas why the above script is failing?
first, in 11g the preferred way is to create a directory and not use utl_file.
secondly, please verify what exact command you used to set the directoty list :
SELECT value
FROM V$PARAMETER
WHERE NAME = 'utl_file_dir';
/appl/mydir, /appl/mydir2, /appl/mydir3
was it
alter system set utl_file_dir='/appl/mydir, /appl/mydir2, /appl/mydir3' scope = spfile;
or
alter system set utl_file_dir='/appl/mydir','/appl/mydir2','/appl/mydir3' scope = spfile;
if its the first way, redo it again the 2nd way as the first way is wrong (it will look the same in the v$table output, but its wrong).
eg:
declare
2
l_file_handle UTL_FILE.FILE_TYPE;
4
begin
l_file_handle := UTL_FILE.FOPEN('/tmp/foo/a',
'/tmp/foo/a/filename.txt',
'w');
9
UTL_FILE.FCLOSE(l_file_handle);
11
DBMS_OUTPUT.PUT_LINE('FINISHED');
13
end;
15 /
declare
*
ERROR at line 1:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at line 6
SQL> show parameter utl_fil
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
utl_file_dir string /tmp/foo, /tmp/foo/a
humm. now lets fix that data.
SQL> alter system set utl_file_dir='/tmp/foo','/tmp/foo/a' scope = spfile;
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup open
ORACLE instance started.
Total System Global Area 263049216 bytes
Fixed Size 2225584 bytes
Variable Size 176163408 bytes
Database Buffers 79691776 bytes
Redo Buffers 4968448 bytes
Database mounted.
Database opened.
declare
2
l_file_handle UTL_FILE.FILE_TYPE;
4
begin
l_file_handle := UTL_FILE.FOPEN('/tmp/foo/a',
'/tmp/foo/a/filename.txt',
'w');
9
UTL_FILE.FCLOSE(l_file_handle);
11
DBMS_OUTPUT.PUT_LINE('FINISHED');
13
end;
15 /
PL/SQL procedure successfully completed.
SQL> show parameter utl_file
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
utl_file_dir string /tmp/foo, /tmp/foo/a
SQL>
also verify the oracle user has r+x on /appl and rwx on /appl/mydir
2 resolution steps for overcoming this error::
1.Be sure that the OS level user has permission to write to a folder
2.Try upper case letters for the directory name i.e. instead of out_dir use OUT_DIR
Out of space may also lead to this issue.
Oracle throws same error if you do not have space on mount point.
ORA-29280: invalid directory path ORA-06512: at "SYS.UTL_FILE", line
41
Please check whether space is available or not on utl_file_dir mountpoint.
For Linux & Solaris use :
df -k ${UTIL_FILE_PATH}
I faced same issue , later got to know it was space issue on mount point.
For the filename put just the name not the path.
So you should put
declare
l_file_handle UTL_FILE.FILE_TYPE;
begin
l_file_handle := UTL_FILE.FOPEN('/appl/mydir',
',filename',
'W');
UTL_FILE.FCLOSE(l_file_handle);
DBMS_OUTPUT.PUT_LINE('FINISHED');
end;

Cannot open file using dbms_lob.filopen but can open it using utl_file

What could cause the following behaviour:
Database 11gR2
declare
l_amt number := dbms_lob.lobmaxsize;
l_dst_loc clob;
l_dst_offset number := 1;
l_lang_ctx number := dbms_lob.default_lang_ctx;
l_src_loc bfile;
l_src_offset number := 1;
l_warning number;
begin
l_src_loc := bfilename('ODS_SERVER_DIRECTORY', '_CIVKD_ASU.CSV');
dbms_lob.createtemporary(l_dst_loc, true);
dbms_lob.fileopen(l_src_loc, dbms_lob.file_readonly);
dbms_lob.loadclobfromfile(l_dst_loc
,l_src_loc
,l_amt
,l_dst_offset
,l_src_offset
,dbms_lob.default_csid
,l_lang_ctx
,l_warning);
commit;
dbms_lob.fileclose(l_src_loc);
dbms_output.put_line(substr(l_dst_loc, 1, 200));
end;
/
ORA-22288: file or LOB operation FILEOPEN failed
.
ORA-06512: in "SYS.DBMS_LOB", line 805
ORA-06512: in line 31
22288. 00000 - "file or LOB operation %s failed\n%s"
*Cause: The operation attempted on the file or LOB failed.
*Action: See the next error message in the error stack for more detailed
information. Also, verify that the file or LOB exists and that
the necessary privileges are set for the specified operation. If
the error still persists, report the error to the DBA.
However opening and reading the exact same file succeeds when using utl_file.
declare
l_file utl_file.file_type;
l_regel varchar2(4000);
begin
l_file := utl_file.fopen('ODS_SERVER_DIRECTORY', '_CIVKD_ASU.CSV', 'R');
-- Haal de volgende regel op
utl_file.get_line(l_file, l_regel);
dbms_output.put_line(l_regel);
utl_file.fclose_all;
end;
So it seems the file is available and accessable by the database.
It's the first time we run into this particular error and it's one of the first 11gR2 instances so maybe there is something 11g specific we don't know about?
=== Update 8-6-2012 ===
Some progress made. It turns out the directory object points to a shared drive. It's a windows server and Oracle runs as Local System. I always thought that it was impossible to read anything from a shared drive in this situation. Apparently in some situations you can using utl_file but not usign dbms_lob.
Could you confirm that ODS_SERVER_DIRECTORY is an actual DIRECTORY
SELECT
*
FROM
dba_objects
WHERE
object_type = 'DIRECTORY'
AND object_name = 'ODS_SERVER_DIRECTORY'
Possibly you have it set in the UTL_FILE_DIR parameter of init.ora (should not be done anymore..)
But its one possibility as to why utl_file would see the directory and dbms_lob would not.

Resources