我在 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
试试这个:-
更新表设置列=replace(column,'hello','abcded')
尝试这个有用的解决方案。 该脚本更新两个字符串之间的字符串。例如你有 xml 文件中的一个唯一标记。您可以更改此标签值,如下所示。 如果您想要更改多个标签,则需要编辑此脚本。因为这个脚本只找到第一个相等。
UPDATE table_name s
SET s.clobcolumn = substr(s.clobcolumn, 1, to_number(dbms_lob.instr(s.clobcolumn, '<firststring>')) + length('<firststring>')-1) || 'replacestring' ||
substr(s.clobcolumn, to_number(dbms_lob.instr(s.clobcolumn, '</secondstring>')) , length(s.clobcolumn))
WHERE ...
AND ...
AND ...;
SELECT to_char(substr(s.clobcolumn,
dbms_lob.instr(s.clobcolumn, '<firststring>'),
(dbms_lob.instr(s.clobcolumn, '</secondstring>') + length('</secondstring>')) - dbms_lob.instr(s.clobcolumn, '<firststring>'))) replaced_string
FROM table_name s
WHERE ...
AND ...
AND ...;
在 SQLDeveloper 中,您可以通过单击架构表并选择编辑来编辑 CLOB。在数据选项卡中,您可以生成 clob 的更新语句。
试试这个:-
DECLARE
str varchar2(32767);
BEGIN
str := '<big-string>';
update <table-name> set <col-name-1>= str where <col-name-2>= ;
END;