oracle, clob type query prompts: 1. The string buffer is too small 2. The data type is inconsistent: it should be -, but the solution of CLOB is obtained

Scenarios where the problem occurs:

First, the database defines the field type in a certain table as CLOB. When this field is used as a query condition and spliced ​​to the end of the where statement, the following prompt will appear:

Or when converting clob type to char type using a function, it prompts that the buffer is too small:

 

Cause of the problem

First of all, the CLOB type cannot be processed and is directly spliced ​​behind the where statement as a condition for equality.

-- 假设id 为 clob类型 当前查询会报错
select t.id from account t where  t.id = '123';

The reason for the error is:

   "123" is a string, t.id is a CLOB type, and cannot be directly connected.


Secondly, after the CLOB type is converted to string format (varchar2), since varchar2 has an upper limit of 4000 length, it is necessary to limit the length before querying.

Solution:

-- 假设id 为 clob类型 
select t.id from account t where  t.id = '123';

-- 方式一:substr控制长度
select t.id from account t where to_char(substr(t.id,0,4000))t.id = '123';

-- 方式二:dbms_lob.substr控制长度
select t.id from account t where to_char(dbms_lob.substr(t.id,4000,1))t.id = '123';

Of course, if the current clob type variable stores Chinese, since Chinese occupies two characters, the upper limit needs to be divided by 2

-- 假设id 为 clob类型 
select t.id from account t where  t.id = '123';



-- 中文情况 substr 上限除以2
select t.id from account t where to_char(substr(t.id,0,4000))t.id = '123';

Guess you like

Origin blog.csdn.net/qq_44716086/article/details/126612947