oracle数据库查询clob字段(Querying oracle clob column)

工作中遇到此场景,google查询到stackoverflow上的讨论,得到解答,使用内置函数dbms_lob.compare。讨论如下:

Question:

I have a table with a clob column. Searching based on the clob column content needs to be performed. However

select * from aTable where aClobColumn = 'value';

fails but

select * from aTable where aClobColumn like 'value';

seems to workfine. How does oracle handle filtering on a clob column. Does it support only the  'like' clause and not the =,!= etc. Is it the same with other databases like mysql, postgres etc

Also how is this scenario handled in frameworks that implement JPA like hibernate ?

Answer: 

Yes, it's not allowed (this restriction does not affectCLOBs comparison in PL/SQL)

to use comparison operators like=,!=,<>and so on in SQL statements, when trying

to compare twoCLOBcolumns orCLOBcolumn and a character literal, like you do. To be

able to do such comparison in SQL statements, dbms_lob.compare() function can be used.

 select * from aTable where dbms_lob.compare(aClobColumn, 'value') = 0

In the above query, the'value'literal will be implicitly converted to theCLOBdata type.

To avoid implicit conversion, the'value'literal can be explicitly converted to theCLOB

data type usingTO_CLOB()function and then pass in to thecompare()function:

 select * from aTable where dbms_lob.compare(aClobColumn, to_clob('value')) = 0

 

你可能感兴趣的:(oracle,clob)