Link to home
Start Free TrialLog in
Avatar of sandeep_patel
sandeep_patelFlag for United States of America

asked on

CLOB to BLOB

Experts !

I have a table as below.

reports
=====
report_id numeric
report_name varchar2(20)
rep_image clob
rep_image2 blob

At present i have data in first three column i.e there is no data in rep_iamge2. Now I want a single update statement or stored procedure which copy data from rep_image to rep_image2.

I am using oracle 8i

Any solution ?

Regards,
Sandeep
Avatar of MohanKNair
MohanKNair

It cannot be done using a single SQL update statement. You will have to code using PL/SQL. How do you want to convert CLOB to BLOB?
Avatar of Sujith
Hi Sandeep,
10g provides functions like DBMS_LOB.CONVERTTOBLOB to do what u are looking for. In 8i there are no direct ways of doing it. Using a single update to convert clob to blob is not possible.
Meanwhile, you can write a procedure to get it updated.
Find the code below that mimics converttoblob functionality.

create or replace procedure test_proc(p_id in number)
as
 a clob;
 b blob;
 c varchar2(1);
 x raw(20);
begin
 select var1 into a
 from test
 where id = p_id;

 update test
 set var2 = empty_blob()
 where id = p_id
 returning var2 into b;

 loop
  exit when length(a) = 0;
  c := substr(a,1,1);
  a := substr(a,2);
  select hextoraw(ltrim(to_char(ascii(c),'XXX'))) into x
  from dual;
  dbms_lob.writeappend(b, length(c), x);
 end loop;

 update test
 set var2 = b
 where id = p_id;

end;
/

To crosscheck your data you can use this code.
declare
 c clob;
 b blob;
 l_amt number;
  src_offset INTEGER := 1;
  dest_offset INTEGER := 1;
lang_ctx INTEGER := dbms_lob.default_lang_ctx;

 l_csid number;
 l_lang number;
 l_warn number;
begin
 select var2 into b
 from test
 where id = 22;

 dbms_lob.createtemporary(c, true);
 dbms_lob.converttoclob(c, b, DBMS_LOB.LOBMAXSIZE , src_offset , dest_offset , DBMS_LOB.default_csid, lang_ctx , l_warn);
 
 dbms_output.put_line(c);
end;
/

HTH,

Rgds,
Sujith.
Yet I am not clear why do u want to store this info in a BLOB column, wont be of much use anyways.
ASKER CERTIFIED SOLUTION
Avatar of schwertner
schwertner
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial