【问题标题】:ORA-01704: string literal too long error update clob fieldORA-01704: 字符串文字太长错误更新 clob 字段
【发布时间】:2019-01-17 18:50:17
【问题描述】:
您能否建议在 Oracle 中更新 clob 字段的方法?
查询很简单,运行它我得到了
ORA-01704: string literal too long:
Update table_name
Set clob_field=value2
Where column=condition1 and clob_field=value1
范围是用新值更新 clob 列中的值。
谢谢
【问题讨论】:
标签:
oracle
plsql
sql-update
【解决方案1】:
您的代码是某个 PLSql 过程的一部分还是简单的 SQL 语句?您将变量“value2”作为绑定变量传递还是在查询中引用字符串?您使用的是 12c 还是早期版本的 Oracle DB?
一般来说,最常见的“不明显”问题与 varchar2 类型在 SQL 语句中限制为 4000 个字符有关。如果您在 PLSql 程序中,则限制为 32K 个字符。
你能提供代码示例吗?事实上,以下两个语句会导致不同的行为:
update table_name
set clob_field=value2
where column=condition1
and clob_field=value1
update table_name
set clob_field='Some very long string.....end of very long string'
where column=condition1
and clob_field='Some even longer string.....end of even longer string'
查看帖子Error : ORA-01704: string literal too long - 示例如何将更新放入 plsql 块中以达到 32.767 个字符的限制。
编辑:
也看看Working with very large text data and CLOB column的帖子
【解决方案2】:
您可能知道,使用 Oracle 数据库在 clob 字段中一次插入的字符不能超过 4k。
解决此问题的解决方法是将整个字符串拆分为 2 个字符串
例子:
create table t_test (id number, texte clob);
insert into t_test (id, texte) values(1, to_clob ('value of 3999 characters') || to_clob ('value of remaining 1001 characters'));
您可以使用“Lorem ipsum”进行测试 :-)
【解决方案3】:
首先将您的字符串值放入 CLOB 变量中:
declare
c clob;
s varchar2(4000);
begin
s:=rpad('x',4000,'x');
for i in 1..100 loop
c:=c||s;
end loop;
dbms_output.put_line( dbms_lob.getlength(c) );
-- length of "c" is 400000 now
update table_name set clob_field=c where id=12345;
end;
/