0
create table t ( x int, y clob );

create or replace procedure p( p_x in int, p_new_text in varchar2 )
    as
    begin
            insert into t values ( p_x, p_new_text );
    end;
    /

exec p(1, rpad('*',4002,'*') );

exec p(2, rpad('é',4002,'é') );

select x, dbms_lob.getlength(y) from t;

select x, dbms_lob.substr(y,4000,1) from t where x = 1; -- this one does not give error and function correctly finds 4000 characters as each character 1 byte

select x, dbms_lob.substr(y,4000,1) from t where x = 2; -- function not able to find 4000 characters and gives error as é is more than 1 byte.
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 1

问题是当 clob 字段中可能有一些字符占用超过 1 个字节时,如何从 oracle 中找到 4000 个字符。

4

1 回答 1

3

问题不在于substr功能。它是 SQL 中 a 的最大长度varchar2:4000字节

这意味着即使这个语句也会失败:

select x, dbms_lob.substr(y,2001,1) from t where x = 2

这是真的,因为两个字节的 2001 个字符是 4002 个字节,这超出了数据类型可以处理的范围。对于这些情况,您应该使用clob.

于 2015-07-19T15:05:16.693 回答