fatcodeguy
Programmer
Hi, I have a table with a CLOB column. some of the CLOBs have string segments that I need to replace, so I wrote a procedure that will get every clob for the column that has this segment that needs to be replaced and update it.
My proc works by creating a temporary clob, reading the clob from the table into the buffer, replacing the string if it exists in the buffer, and then writing the buffer to the temporary clob. Then i update the table with the new temp clob.
My problem is that for some of the clobs (in no pattern that I can discern) the last character is ommitted.
Here's my procedure, can anyone help me?
Thanks
My proc works by creating a temporary clob, reading the clob from the table into the buffer, replacing the string if it exists in the buffer, and then writing the buffer to the temporary clob. Then i update the table with the new temp clob.
My problem is that for some of the clobs (in no pattern that I can discern) the last character is ommitted.
Here's my procedure, can anyone help me?
Code:
procedure replace_clob(p_id sp_doc.id%type) IS
v_enclob CLOB := EMPTY_CLOB;
v_new_clob CLOB := EMPTY_CLOB;
v_buffer VARCHAR2 (32767);
l_amount BINARY_INTEGER := 32767;
l_pos PLS_INTEGER := 1;
l_clob_len PLS_INTEGER;
cursor clobs is
select id,display_order,enbody,frbody
from sp_doc
where id=p_id
order by display_order;
BEGIN
for v in clobs loop
--get enbody into variable
v_enclob:=v.enbody;
--initialize the new clob
dbms_lob.CreateTemporary(v_new_clob,TRUE);
--reset other values
v_buffer:='';
l_pos:=1;
--get the length of the clob
l_clob_len := dbms_lob.getlength(v_enclob);
while l_pos < l_clob_len loop
--read from source, into the buffer
dbms_lob.read(v_enclob, l_amount, l_pos, v_buffer);
if v_buffer is not null then
--replace the text we want to replace
v_buffer:=replace(v_buffer,'find_str','replace_str');
--write to the new clob
dbms_lob.writeAppend(v_new_clob, length(v_buffer), v_buffer);
end if;
--increment counter
l_pos := l_pos + l_amount;
--clear buffer
v_buffer:='';
end loop;
--now we have the new clob ... update the table
update sp_doc set enbody=v_new_clob where id=v.id and display_order=v.display_order;
end loop;
EXCEPTION
WHEN others THEN
dbms_output.put_line('ERROR');
end;
Thanks