Anda di halaman 1dari 7

Ask Tom "Long Raw to BLOB"

Page 1 of 7

Home > Question Details







Firas -- Thanks for the question regarding "Long Raw to BLOB", version 8.1.7
Submitted on 22-Oct-2003 19:01 Central time zone Last updated 5-Apr-2010 10:23 Tom's latest followup | Bookmark | Bottom

You Asked
Hi Tom, We are using an Oracle 8.1.7 database. Is there a way in PL/SQL or Java Stored Procedure to convert a Long Raw into a BLOB? Thanks, Firas Khasawneh

and we said...
ops$tkyte@ORA817DEV> create table gtt ( id int, x blob ); Table created. ops$tkyte@ORA817DEV> ops$tkyte@ORA817DEV> create table t ( id int primary key, x long raw ); Table created. ops$tkyte@ORA817DEV> ops$tkyte@ORA817DEV> insert into t values( 1, rpad( 'a', 2000, 'a' ) ); 1 row created. ops$tkyte@ORA817DEV> ops$tkyte@ORA817DEV> ops$tkyte@ORA817DEV> declare 2 l_id number := 1; 3 l_blob blob; 4 begin 5 execute immediate ' 6 insert into gtt 7 select :x, to_lob(x) 8 from t 9 where id = :x' using l_id, l_id; 10 11 select x into l_blob from gtt where id = l_id; 12 end; 13 / PL/SQL procedure successfully completed. if you just want to convert single row for a session. use

create table new_table as select ...., to_lob(long_raw) from old_table to permanently convert all rows.

Reviews Excellent, Thanks October 23, 2003 - 3pm Central time zone Reviewer: Firas from NC USA Hi Tom, Thanks a lot for your prompt response. Is it possible to update a Long Raw column with a BLOB value? I read some where that that is possible by converting the binary bytes into hex but I am not sure if that is possible or how to do it. Any suggestions is highly appreciated. Bookmark | Bottom | Top

Followup October 23, 2003 - 7pm Central time zone:

nope, no can do there. You would have to use the API's to update the long raw (if the long raw/blob is over 32k, if 32k and less then yes, plsql can do it)


Ask Tom "Long Raw to BLOB"

Page 2 of 7

April 19, 2005 - 9am Central time zone Reviewer: A reader Hi Tom, I have tables x and y as create table x ( id number, str varchar2(255), lrimage long_raw ); create table y ( id number, str varchar2(255), blimage blob );

Bookmark | Bottom | Top

Table x has about 11 million rows and I am trying to move data from table x to table y. I tried the following: create or replace procedure procblob as tstr varchar2(255); timage blob; begin .......... select str, to_lob(lrimage) into tstr, timage from x where id = 1; insert into y values (1, tstr, timage); ..... end; / When I try to compile this procedure I am getting the error PL/SQL: ORA-00932: inconsistent datatypes: expected - got BINARY I also tried create or replace procedure procblob as tstr varchar2(255); timage long raw; begin .......... select str, lrimage into tstr, timage from x where id = 1; insert into y values (1, tstr, to_lob(timage)); ..... end; / Again I am getting the same error. Please let me know what to do. Thanks.

Followup April 19, 2005 - 9am Central time zone:

use INSERT INTO select .... do not fetch out, put into plsql only to send back.

insert into y select str, to_lob(lrimage) from x where ...; no plsql, just sql.

April 19, 2005 - 10am Central time zone Reviewer: A reader

Bookmark | Bottom | Top


Ask Tom "Long Raw to BLOB"

Page 3 of 7

Thank you Tom it works great.

Does it work in too? May 2, 2006 - 7am Central time zone Reviewer: Roland Rottlaender from Germany Hi Tom, is there a difference for I have a table blobdata( ownerid longid segmentid segment ) But a Query Select longid, to_lob(segment) from blobdata where longid = 12345 returns ORA-00932: inconsistent datatypes: expected - got BINARY

Bookmark | Bottom | Top

number(10), number(10), number(10), LONG RAW

Then I create table blobdata_temp( ownerid NUMBER(10), longid NUMBER(10), segmentid NUMBER(10), segment BLOB ) and tried INSERT INTO blobdata_temp SELECT ownerid, longid, segmentid, to_lob(segment) from blobdata where ... All I got is ORA-00997: illegal use of LONG datatype I really has no idea what's going wrong... Would be great if you could help me... Thanks, Roland

Followup May 2, 2006 - 7am Central time zone:

to_lob only works on insert as select. give me a cut and pasted example. something like this from my 9206 database: ops$tkyte@ORA9IR2> create table blobdata( 2 ownerid number(10), 3 longid number(10), 4 segmentid number(10), 5 segment LONG RAW 6 ); Table created. ops$tkyte@ORA9IR2> insert into blobdata values ( 1, 2, 3, rpad( '00', 4000, '0' ) ); 1 row created. ops$tkyte@ORA9IR2> declare 2 l_raw long raw; 3 begin 4 l_raw := rpad( '00', 32766, '0' ); 5 insert into blobdata values ( 1, 2, 3, l_raw ); 6 end; 7 / PL/SQL procedure successfully completed.


Ask Tom "Long Raw to BLOB"

Page 4 of 7

ops$tkyte@ORA9IR2> ops$tkyte@ORA9IR2> create table blobdata_temp( 2 ownerid NUMBER(10), 3 longid NUMBER(10), 4 segmentid NUMBER(10), 5 segment BLOB 6 ); Table created. ops$tkyte@ORA9IR2> ops$tkyte@ORA9IR2> INSERT INTO blobdata_temp 2 SELECT ownerid, longid, segmentid, to_lob(segment) 3 from blobdata; 2 rows created. ops$tkyte@ORA9IR2> ops$tkyte@ORA9IR2> select dbms_lob.getlength( segment) from blobdata_temp; DBMS_LOB.GETLENGTH(SEGMENT) --------------------------2000 16383 ops$tkyte@ORA9IR2>

September 4, 2008 - 10am Central time zone Reviewer: Visi from Albania

Bookmark | Bottom | Top

Hi Tom, I have been visiting your site so many times, but it is only this time I am taking the chance to make a question regarding the convertion of Long Raw to BLOB. I am in the process of loading some 40(GB)pictures from a MSSQL system to Oracle 10g R2 using OLEDB though heterogeneus system(HS). This is my target table in oracle: create table T_PICTURES ( IMAGE_ID VARCHAR2(10) not null, FILENAME VARCHAR2(250), ACTIONNR NUMBER, PICTURE BLOB, BYTES VARCHAR2(10), INFO VARCHAR2(10), IMAGEDATE DATE ); alter table T_PICTURES add constraint XPK_PICTURES primary key (IMAGE_ID)); And this is the description from sqlplus of the remote table in MSSQL. image_id varchar2(8) filename varchar2(250) actionnr number(10) y picture long raw y bytes number(10) y info number(10) y imagedate date y

The following is a code I wrote and it is suposed to migrate a record at a time, any time it is being executed and converting the LONG RAW picture field to BLOB using the TO_LOB function. begin for c in (select from where and and loop

t.appno stg_passports t, t_pictures p t.appno = p.image_id(+) p.image_id is null rownum <= 1 )

insert into t_pictures (image_id, filename, actionnr, picture, bytes, info, imagedate) SELECT "ImageID" image_id, "FileName" filename, "ActionNr" actionnr, to_lob("Picture") picture, "Bytes" bytes, "Info" info, "ImageDate" imagedate


Ask Tom "Long Raw to BLOB"

Page 5 of 7

FROM ITCPortrait@mssql WHERE "ImageID" = c.appno; commit; end loop; -end; When I try to run it, I am getting ORA-00997: illegal use of LONG datatype ORA-06512: at line 10 Am I doing something wrong here? I am realy confused. I'd really appreciate your intervention on this. I hope I was somehow clear in my explanations. Thanks and regards, Visi

Followup September 4, 2008 - 1pm Central time zone:

to_lob is not implemented by mysql - it would be the thing that would have to convert the long raw into a lob. long raws are not going to be dblink friendly at all. Your best bet will be to write an external application that selects from mysql, inserts into Oracle. Or dump the data out into a flat file and load that. September 5, 2008 - 7am Central time zone Reviewer: Visi from Albania Hi Tom, Sorry for the confusion. By MSSQL I ment Microsoft SQL Server not MYSQL. However, I think I was able to overcome the issue by introducing a middle step in the procedure. Instead of using an INSERT...INTO clause with a TO_LOB function, I used a record type Variable as a helper. This is the final code of the procedure: create or replace procedure p_load_picture as type TPicData is record( image_id varchar2(8), filename varchar2(250), actionnr number(10), picture long raw, bytes number(10), info number(10), imagedate date); -v_data TPicData; -begin for c in (select t.appno from stg_passports t, t_pictures p where t.appno = p.image_id(+) and p.image_id is null and rownum <= 1) loop SELECT "ImageID" image_id, "FileName" filename, "ActionNr" actionnr, "Picture" picture, "Bytes" bytes, "Info" info, "ImageDate" imagedate into v_data FROM ITCPortrait@mssql where "ImageID" = c.appno; -insert into t_pictures (image_id, filename, actionnr, picture, bytes, info, imagedate) values (v_data.image_id, v_data.filename, v_data.actionnr, v_data.picture, v_data.bytes,, v_data.imagedate); -end loop; -commit; -end; Amazingly, Oracle is implicitly converting the LONG RAW data type to BLOB data type, so at the end we don't need to call a TO_LOB function at all. Bookmark | Bottom | Top


Ask Tom "Long Raw to BLOB"

Page 6 of 7

Note: The LOOP might look unnecessary there but probably I am going to increase the number of records loaded in a single run. Thanks again Tom Regards, Visi

Followup September 5, 2008 - 9am Central time zone:

do you have any images over 32k, if so, there will be a problem there.. September 5, 2008 - 11am Central time zone Reviewer: Visi from Albania Bookmark | Bottom | Top

Hi Tom, The avarege size of a picture is about 10KB.The largest foto so far looks to be around 20KB. Probably I was lucky :) Thanks for letting me know. Regards, Visi

Long raw March 6, 2009 - 3am Central time zone Reviewer: Anis from Dhaka, Bangladesh

Bookmark | Bottom | Top

Would you help me, I want to insert doc or pdf file into oracle database from oracle developer 6i. Thanks Anis

Followup March 6, 2009 - 10am Central time zone: -> discussion forums -> there are good ones for Developer related questions. I haven't touched it since 1995. TO_LOB() doubt March 3, 2010 - 9am Central time zone Reviewer: Badri from India Hi Tom, I have a table t with a LOB column into which i tried the below query, insert into t select to_lob(text) from user_views; Bookmark | Bottom | Top

and this worked. But when I tried to execute only select to_lob(text) from user_views;

I got the below error SQL Error: ORA-00932: inconsistent datatypes: expected - got LONG 00932. 00000 - "inconsistent datatypes: expected %s got %s"

Could you please help me understand the reason for this behaviour. Thanks in advance Badri

Followup March 3, 2010 - 10am Central time zone:

to_lob() is documented to only work in an insert/select or create table/select statement.

The lob needs to "live" somewhere, with just a select - the lob wouldn't exist in a table anywhere. The to_lob function was implemented purely to convert a long to a lob - once (as a migration device). That was the intended functionality.


Ask Tom "Long Raw to BLOB"

Page 7 of 7

raw March 3, 2010 - 10pm Central time zone Reviewer: A reader

Bookmark | Bottom | Top

Can you explain to us the difference between BLOB and RAW datatype and for what kind of data you use RAW data type.

Followup March 4, 2010 - 9am Central time zone:

RAW is limited to 2000 bytes. blob is virtually unlimited (terabytes). if you have data that is binary in nature (not a string - just binary bytes) that is 2000 bytes or less - use RAW (more efficient, more compact, less overhead) if you have data that is binary in nature that exceeds 2000 bytes, you will use a blob - which can store massive amounts of data. March 29, 2010 - 11pm Central time zone Reviewer: Ram from Dallas, TX USA INSERT INTO APAC2.PS_VENDOR_CONVER (SELECT DISTINCT 'MASTR', A.VENDOR_ID, A.CONVER_DT, A.CONVER_SEQ_NUM, A.CNTCT_SEQ_NUM, A.CONVER_TOPIC, A.OPRID, A.REVIEW_DAYS, A.REVIEW_DATE, A.REVIEW_NEXT_DATE, A.KEYWORD1, A.KEYWORD2, A.KEYWORD3, TO_LOB (A.DESCRLONG) FROM APAC2.PS_C_NR_VDCNVR_TMP A) A.DESCRLONG is a LONG field, so I am using TO_LOB Hi Tom I need help here wrt LONG field. In the SELECT I have to use a DISTINCT becos of functionality and I have to use the Long Field as well. A.DESCRLONG is a LONG field. But this is showing me an error that it is a wrong data type. Can you help me in resolving the sql insert please Thanks Bookmark | Bottom | Top

Followup April 5, 2010 - 10am Central time zone:

you cannot use distinct, it will simply not work - there is no "solution" using straight SQL. longs cannot be distinct'ed... Now, if it is some set of fields that should be distincted you could: select .... existing select list.... from table where rowid in (select max(rowid) from table group by <that list of columns>);

That would work - if the descrlong is not to be in the list of distincted columns. If it is, you will not be using SQL only to do this.

Write a Review

All information and materials provided here are provided "as-is"; Oracle disclaims all express and implied warranties, including, the implied warranties of merchantability or fitness for a particular use. Oracle shall not be liable for any damages, including, direct, indirect, incidental, special or consequential damages for loss of profits, revenue, data or data use, incurred by you or any third party in connection with the use of this information or these materials.

About Oracle | Legal Notices and Terms of Use | Privacy Statement