繁体   English   中英

如何更新包含 oracle 中 XML 数据的 blob 列

[英]how to update a blob column containing XML data in oracle

我尝试了以下两种方法:

  1. update table set blobcolname='<?xml>...';

收到此错误:“字符串文字工具长” - 因为我插入的 xml 值是工具长

2.

DECLARE
str varchar2(32767);
BEGIN
str:='<?xml>...';
update table set blobcolname = str;
END;
/

收到此错误:ORA-01461:can bind a LONG value only for insert into a LONG column

如果您可以选择在数据库中安装两个函数,我会使用这个:

首先:我创建了一个 function 来将 clob 转换为 blob object

create or replace function clob_to_blob (p1_clob CLOB) return BLOB is
  Result BLOB;
  o1 integer;
  o2 integer;
  c integer;
  w integer;
begin
  o1 := 1;
  o2 := 1;
  c := 0;
  w := 0;
  DBMS_LOB.CreateTemporary(Result, true);
  DBMS_LOB.ConvertToBlob(Result, p1_clob, length(p1_clob), o1, o2, 0, c, w);
  return(Result);
end clob2blob;
/

第二:我创建一个 function 到相反,将 blob 转换为 clob

create or replace function blob_to_char (p1_blob BLOB)
return clob is
  out_clob clob;
  n number;
begin
  if (p1_blob is null) then
    return null;
  end if;
  if (length(p1_blob)=0) then
    return empty_clob();
  end if;
  dbms_lob.createtemporary(out_clob,true);
  n:=1;
  while (n+32767<=length(p1_blob)) loop
    dbms_lob.writeappend(out_clob,32767,utl_raw.cast_to_varchar2(dbms_lob.substr(p1_blob,32767,n)));
    n:=n+32767;
  end loop;
  dbms_lob.writeappend(out_clob,length(p1_blob)-n+1,utl_raw.cast_to_varchar2(dbms_lob.substr(p1_blob,length(p1_blob)-n+1,n)));
  return out_clob;
end;
/

第三:验证我可以插入

declare
str clob;
BEGIN
str :='<?xml>...';
update table set blobcolname = clob_to_blob ( str );
END;
/

最后:进行回归测试并检查我是否得到了原来的 xml

select blob_to_char( blobcolname ) from your table;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM