Skip to Main Content
  • Questions
  • Error when trying to view BFILE data

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Thiruvaiyaru.

Asked: August 24, 2000 - 3:09 pm UTC

Last updated: January 06, 2011 - 8:03 am UTC

Version: 8.1.6

Viewed 10K+ times! This question is

You Asked

Tom,

This is regarding the question that I had asked :-

Q (1): Viewing Data associated with BLOB Datatype
on August 14th.

I created a table called "test" with 2 columns
empno number primary key
details binary file LOB

I created a directory as follows :-

create directory test_dir as '/home/test';

and then inserted a row to the table :-
insert into test values(1111,BFILENAME('test_dir','image.txt'));

the file image.txt resides on /home/test directory.

I tried to view the data using the following select stmnt :-

select utl_raw.cast_to_varchar2(dbms_lob.substr(details,1,100))
from test;

The result gives the following errors :-

ERROR at line 1:
ORA-22289: cannot perform FILEREAD operation on an unopened file or LOB
ORA-06512: at "SYS.DBMS_LOB", line 680
ORA-06512: at line 1

Your help in this would be greatly appreciated.

Thanks
Sai






and Tom said...

You need to write one function -- something like "blobtochar" in the following:

ops$tkyte@8i> create table demo
2 ( id int primary key,
3 theBlob bfile
4 )
5 /

Table created.

ops$tkyte@8i>
ops$tkyte@8i> create or replace directory
2 my_files as '/export/home/tkyte/public_html';

Directory created.

ops$tkyte@8i>
ops$tkyte@8i> insert into demo
2 values
3 ( 1, bfilename( 'MY_FILES', 'blob.sql' ) );

1 row created.

ops$tkyte@8i>
ops$tkyte@8i>
ops$tkyte@8i> create or replace
2 function blobtochar( p_blob in bfile ) return varchar2
3 as
4 l_raw raw(4000);
5 l_bfile bfile default p_blob;
6 begin
7 dbms_lob.fileopen( l_bfile );
8 l_raw := dbms_lob.substr( l_bfile,4000,1 );
9 dbms_lob.fileclose( l_bfile );
10 return utl_raw.cast_to_varchar2(l_raw);
11 end;
12 /

Function created.

ops$tkyte@8i>
ops$tkyte@8i> column x format a40 word_wrapped
ops$tkyte@8i> select id, blobtochar(theBlob) x from demo
2 /

ID X
---------- ----------------------------------------
1 drop table demo;
create table demo
( id int primary key,
theBlob bfile
)
/
create or replace directory
my_files as
'/export/home/tkyte/public_html';
insert into demo
values
( 1, bfilename( 'MY_FILES', 'blob.sql' )
);
create or replace
function blobtochar( p_blob in bfile )
return varchar2
as
l_raw raw(4000);
l_bfile bfile default p_blob;
begin
dbms_lob.fileopen( l_bfile );
l_raw := dbms_lob.substr( l_bfile,4000,1
);
dbms_lob.fileclose( l_bfile );
return utl_raw.cast_to_varchar2(l_raw);
end;
/
column x format a40 word_wrapped
select id, blobtochar(theBlob) x from
demo
/


Rating

  (15 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

I got the error message like this ora-22288 and ora-06512

Ram Mohan, February 23, 2002 - 4:12 am UTC

ORA-22288: file or LOB operation FILEOPEN failed
LFI-00108: Open expect the file [blob][sql] to exist.
ORA-06512: at "SYS.DBMS_LOB", line 475
ORA-06512: at "SCOTT.BLOBTOCHAR", line 5
ORA-06512: at line 1

Tom Kyte
February 23, 2002 - 10:43 am UTC

seems self explanatory to me.

The file you requested to open to read does not exist on the SERVER in the directory you specified.

Make sure you are not using a "network" drive on NT. They are not visible to the database by default (it is not like a Unix "mount" in NT, the network drives YOU see are not the same network drives services and other background processes 'see').

Make sure the file actually EXISTS on the server, the database server can only read files that exist on the server (otherwise the CLIENT app you write must read the file and write it to the lob).

Make sure the file on the server is READABLE by the very un-privileged OS account that is uses to run the database service.

Make sure you are using the right directory in the code as well.



how to use network drives to access data

vishwesh, April 24, 2002 - 4:55 am UTC

hi,
i have the dat files and the ctl files for loading data on an application server. i need to load data on the database server. for that i have written a java code which will use the Runtime and exec a sqlldr utility to load data. if i run this java code , the data loads, but if i wrap it as a procedure it doesnt. this is because of the data being on a different machine. how can i access data on a diff machine

Tom Kyte
April 24, 2002 - 7:30 am UTC

On unix -- just mount it and it's there (ahh, the simplicity of Unix).

On NT -- read:

</code> http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:241814624807 <code>

and you'll see what hoops you must jump through.

Load MS doc

Sean, July 11, 2003 - 6:07 pm UTC

Hi Tom,

I am using 9i release 2 on Window 2000.

I used your script. It works fine with Bfile text file. But the display on SQLPLUS is a little strange for MS document of bfile (not surprised me).

My question is how to read MS word in blob or bfile using dbms_lob package? Another question is how to load MS doc into blob without using C, Java or VB.

Thanks so much for your help.

Best,

Sean


Tom Kyte
July 12, 2003 - 9:13 am UTC

dbms_lob.loadfromfile

see the application developers guide for LOBS:

</code> http://docs.oracle.com/docs/cd/B10501_01/nav/docindex.htm#index-APP <code>


or search for

load lob

on this site.

OK

Raju, May 15, 2004 - 7:12 am UTC

Dear Tom,
How to read a bfile containing a Doc. as its content.For example 
I have a table as 

SQL>create table empl(ename varchar2(30),resume bfile);
Table created.

SQL>insert into empl values('Raju',bfilename('D','Raj.doc') );
1 row created.

when I issue like

SQL> select * from empl;

I get the following error:

SP2-0678: Column or attribute type can not be displayed by SQL*Plus

My Question is:

How to read that document? 

Tom Kyte
May 15, 2004 - 12:42 pm UTC

write a program that can display it. sqlplus is just a text based command line interface. it has no clue what to do with a binary ".doc" file.

BFILENAME

Rajesh, December 12, 2007 - 1:32 pm UTC

Hi Tom,

Thanks for all your good work. I would like to know whether BFILENAME function accepts the filename parameter with wildcards (* or %).

I have a file called bt_srs_smmry_12122007.sql in my directory. When I use this filename in BFILENAME function, I would not know the date. So I would like to have something like this...

BFILENAME('DIRNAME','bt_srs_smmry%') which should fetch the file bt_srs_smmry_12122007.sql. I am aware that the above function with the '%' operator will not work. Is there any other way to solve this problem ?

Thanks
Rajesh
Tom Kyte
December 13, 2007 - 8:52 am UTC

No, it does not - it would not make sense. What if there were 100 files that matched?

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:7506780031005

File Search

Rajesh, December 17, 2007 - 10:12 am UTC

Hi Tom,

I have a file of name bt_smmry_12172007142930.xml in my directory. I should check whether the file is present in the directory or not. I also know only the prefix of the filename bt_smmry*.xml.

Can I do this in Oracle ?

Thanks
Rajesh
Tom Kyte
December 17, 2007 - 11:00 am UTC

Rajesh

did you even consider following the link I provided you right above?

It, well, shows you how to get a directory listing from the database...

fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 04, 2011 - 4:24 am UTC

Hi Tom,

I am using Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod.
I am working on loading MS .doc into Oracle.

I have created dir using following code:
BEGIN
EXECUTE IMMEDIATE
'CREATE OR REPLACE DIRECTORY TEST_SN1 AS'||
'''\\inecg-sdc11\XYZ_ABC2\DT-AUTOMATION\DUMMY_12LNEW''';
END;

I have verified the existence of file(Status.doc) using utl_file.fgetattr using the following code:


DECLARE
ex BOOLEAN;
flen NUMBER;
bsize NUMBER;
BEGIN
utl_file.fgetattr('TEST_SN1', 'Status.doc', ex, flen, bsize);

IF ex THEN
insert into test_msg values('File Exists');
ELSE
insert into test_msg values('File Does Not Exist');
END IF;
insert into test_msg values('File Length: ' || TO_CHAR(flen));
insert into test_msg values('Block Size: ' || TO_CHAR(bsize));
commit;
END;

the code succuessfully returns the msg 'File exists' along with the file length .

But the following code still produces error ORA-22288:

declare
l_blob blob;
l_clob clob;
l_id number;
l_bfile bfile;
begin
insert into demo values ( s.nextval, empty_blob(), empty_clob() )
returning id, theblob, theclob into l_id, l_blob, l_clob;

l_bfile := bfilename( 'TEST_SN1', 'Status.doc' );
dbms_lob.fileopen( l_bfile );

dbms_lob.loadfromfile( l_blob, l_bfile, dbms_lob.getlength( l_bfile ) );

dbms_lob.fileclose( l_bfile );

end;
/
ORA-22288: file or LOB operation FILEOPEN failed
ORA-06512: at "SYS.DBMS_LOB", line 523

Even though the file exists and Oracle is able to identify the file, I cant figure out what is stopping my code to open that file.
Please help me to figure out the problem.

Thanks a million tons!
Tom Kyte
January 04, 2011 - 7:15 am UTC

are you sure you have read access to the file. Just because you can see it exists, doesn't mean you are able to open it and see the contents.

fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 04, 2011 - 9:30 am UTC

Hi TOM,

Thanks for your prompt reply.
The file is in readable format.
I want to add one more doubt.Will it make any difference if the directory path I have given is Windows path and the Oracle is installed in the Unix m/c.If yes,then how the UTL_FILE.FGETATTR is working.
Also I have used following code trying to read the file but its returning 'ERROR' in the table through the exception:

DECLARE
vInHandle utl_file.file_type;
vNewLine VARCHAR2(250);
BEGIN
vInHandle:= utl_file.fopen('TEST_SN1', 'Status.doc', 'R');
LOOP
BEGIN
utl_file.get_line(vInHandle, vNewLine);
insert into test_msg values(vNewLine);
commit;
EXCEPTION
WHEN OTHERS THEN
insert into test_msg values('ERROR');
EXIT;
END;
END LOOP;
utl_file.fclose(vInHandle);
END;
-------------------

Please let me know any method to find whether the file is readable or not from Oracle

Thanks!!

Tom Kyte
January 04, 2011 - 10:45 am UTC

what is "unix m/c"??


that commit in the loop scares the heck out of me.

Your exception block is horrible - I hate your code:
http://asktom.oracle.com/pls/asktom/asktom.search?p_string=%22i+hate+your+code%22


The file is obviously not readable, you cannot open it. You should be able to prove that in one line of code:

DECLARE
vInHandle utl_file.file_type;
BEGIN
vInHandle:= utl_file.fopen('TEST_SN1', 'Status.doc', 'R');
end;


if that fails, the file is not available to you.

fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 05, 2011 - 7:51 am UTC

Hi Tom,

The above code is executing successfully.
I am not able to figure out the actual cause of the problem.
Please help.
Tom Kyte
January 05, 2011 - 7:55 am UTC

cut and paste the output of the following code from a sqlplus session - a direct cut and paste - NO EDITS:

declare
    l_bfile bfile;
begin
    l_bfile := bfilename( 'TEST_SN1', 'Status.doc' );
    dbms_lob.fileopen( l_bfile );
    dbms_lob.fileclose( l_bfile );
end;
/

DECLARE
 vInHandle utl_file.file_type;
 vNewLine  VARCHAR2(250);
BEGIN
  vInHandle:= utl_file.fopen('TEST_SN1', 'Status.doc', 'R');
  utl_file.fclose(vInHandle);
END;
/

fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 05, 2011 - 8:25 am UTC

Further to my above response,I have also checked accessing the same file from the machine where Oracle is installed and its accesible.i am able to open and read that file.
Tom Kyte
January 05, 2011 - 8:41 am UTC

but - who were you logged in as ? The account the Oracle software runs as - yourself - the oracle software owner - they are all different with potentially different privileges.

fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 05, 2011 - 8:31 am UTC

the first block is giving the following error:

Error starting at line 1 in command:
declare
l_bfile bfile;
begin
l_bfile := bfilename( 'TEST_SN1', 'Status.doc' );
dbms_lob.fileopen( l_bfile );
dbms_lob.fileclose( l_bfile );
end;
Error report:
ORA-22288: file or LOB operation FILEOPEN failed
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at line 5
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.




the second block results:

anonymous block completed
Tom Kyte
January 05, 2011 - 8:45 am UTC

please do what I said to do which was:


cut and paste the output of the following code from a sqlplus session - a direct cut and paste - NO EDITS:


just like this:

ops$tkyte%ORA11GR2> set echo on
ops$tkyte%ORA11GR2> @test
ops$tkyte%ORA11GR2> declare
  2          l_bfile bfile;
  3  begin
  4          l_bfile := bfilename( 'TEST_SN1', 'Status.doc' );
  5          dbms_lob.fileopen( l_bfile );
  6          dbms_lob.fileclose( l_bfile );
  7  end;
  8  /

PL/SQL procedure successfully completed.

ops$tkyte%ORA11GR2> 
ops$tkyte%ORA11GR2> DECLARE
  2   vInHandle utl_file.file_type;
  3   vNewLine  VARCHAR2(250);
  4  BEGIN
  5    vInHandle:= utl_file.fopen('TEST_SN1', 'Status.doc', 'R');
  6    utl_file.fclose(vInHandle);
  7  END;
  8  /

PL/SQL procedure successfully completed.


fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 06, 2011 - 1:43 am UTC

This is what the output was:

SQL> declare
  2      l_bfile bfile;
  3  begin
  4      l_bfile := bfilename( 'TEST_SN1', 'Status.doc' );
  5      dbms_lob.fileopen( l_bfile );
  6      dbms_lob.fileclose( l_bfile );
  7  end;
  8  /
declare
*
ERROR at line 1:
ORA-22288: file or LOB operation FILEOPEN failed
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at line 5


SQL> 
SQL> DECLARE
  2   vInHandle utl_file.file_type;
  3   vNewLine  VARCHAR2(250);
  4  BEGIN
  5    vInHandle:= utl_file.fopen('TEST_SN1', 'Status.doc', 'R');
  6    utl_file.fclose(vInHandle);
  7  END;
  8  /

PL/SQL procedure successfully completed.

Tom Kyte
January 06, 2011 - 8:03 am UTC

I'll have to refer you to support now - you have the *perfect* test case - small, simple, concise yet 100% complete - shows everything.

There is nothing I can think of that would prevent the first block from working.

fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 06, 2011 - 3:41 am UTC

Hi Tom,

I tried loading the document from the local drive itself and I SUCCEEDED.
The problem may be in reading the file from remote drive.But I am not able to figure out the actual reason and also the rectfication of the same.
Also, why UTL_FILE is not giving any errors but the dbms_lob.fileopen is giving?

Thanks!

fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 06, 2011 - 8:37 am UTC

THANKS A LOT TOM!!!
Thanks all for your support and guidance.

fileopen failed though utl_file.fgetattr works fine

Saurabh Nolakha, January 06, 2011 - 8:38 am UTC

THANKS A LOT TOM!!!
Thanks for all your support and guidance.

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here