我在 Oracle 数据库中有一个表,其字段的数据类型为 CLOB
。字段名称为XMLString
。我正在存储每条记录 10,000 个字符长的 XML 字符串。我在这个表中有超过 100, 000 条记录。
我需要在特定位置更新每条记录上的 XML 字符串段。例如,我需要使用“My New text”之类的字符串更新第 14 个位置的每条记录。此替换文本的长度为 11 个字符。所以这仅仅意味着它将替换从第 14 个字符开始的 11 个字符。
我尝试使用DBMS_LOB.FRAGMENT_REPLACE
,但这并不完全是我想要的。
有没有像
这样简单的命令Replace(XMLString, 14, ‘My New text’)
这样我就可以做如下的事情?
UPDATE MYTABLE
SET MyClobField = Replace(MyClobField, 14, 'My New text')
WHERE MyTableID>5000
如有任何帮助,我们将不胜感激。
最佳答案
使用 SQL
UPDATE MYTABLE
SET MyClobField = substr(MyClobField, 1, 10) || to_clob('MyNewtext')||substr(MyClobField, 10+length('MyNewtext')+1)
where..
只需将出现的 2 次“10”更改为偏移量即可。
或者在 PL/SQL 中使用 DBMS_LOB.WRITE API(这比上面更快)
SQL> create table foo(c clob);
Table created.
SQL> insert into foo values ( 'this is a test string ' || rpad('x', 20, 'x'));
1 row created.
SQL> commit;
Commit complete.
SQL> select * from foo;
C
--------------------------------------------------------------------------------
this is a test string xxxxxxxxxxxxxxxxxxxx
SQL> declare
2 v_lob clob;
3 begin
4
5 for r_lob in (select c
6 from foo
7 for update)
8 loop
9 dbms_lob.write(r_lob.c, 6, 16, 'phrase'); -- ie write at offset 16, 6 bytes
10 end loop;
11 end;
12 /
PL/SQL procedure successfully completed.
SQL> select * from foo;
C
--------------------------------------------------------------------------------
this is a test phrase xxxxxxxxxxxxxxxxxxxx
关于oracle - 更新Oracle中的CLOB字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13568932/