暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

How to avoid "ORA-00932: inconsistent datatypes: expected - got CLOB" when distinct CLOB datatypes.

原创 Anbob 2013-12-25
832
In Oracle, you can't directly use "distinct" in queries on tables with CLOB types.

anbob@ANBOB>create table t(id int,c clob);
Table created.
anbob@ANBOB>insert into t values (1,'a');
1 row created.
anbob@ANBOB>select * from t;
ID C
-------------------- --------------
1 a

anbob@ANBOB>select distinct id,c from t;
select distinct id,c from t
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CLOB
anbob@ANBOB>select distinct c from t;
select distinct c from t
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CLOB
anbob@ANBOB>create view v as select distinct id,c from t;
create view v as select distinct id,c from t
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CLOB
复制

If your values are always less than 4k, you can use:

anbob@ANBOB>create or replace view v as select distinct id,to_char(c) new_c from t;
View created.
anbob@ANBOB>select * from v;
ID NEW_C
-------------------- ----------
1 a
-- where
anbob@ANBOB>select * from t where c='a';
select * from t where c='a'
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CLOB
复制

Tip:
Comparison conditions compare one expression with another. The result of such a comparison can be TRUE, FALSE, or UNKNOWN.
Large objects (LOBs) are not supported in comparison conditions. However, you can use PL/SQL programs for comparisons on CLOB data.
read more oracle doc.
Another way, You can too use this function to avoid the error dbms_lob.substr()

anbob@ANBOB>select distinct dbms_lob.substr(c) new_c from t;
NEW_C
----------
a
复制

Another way, use pl/sql to avoid the error

anbob@ANBOB>create or replace type typ_clob
2 is object (c clob,order member function equals(p_c typ_clob)
3 return number);
4 /
Type created.
anbob@ANBOB>create or replace type body typ_clob
2 is
3 order member function equals(p_c typ_clob) return number
4 is
5 begin
6 return case when self.c is null and p_c.c is null then 0
7 else nvl(dbms_lob.compare(self.c,p_c.c),1)
8 end;
9 end;
10 end;
11 /
Type body created.
anbob@ANBOB>select * from t;
ID C
-------------------- ----------
1 a
anbob@ANBOB>insert into t values (2,'a');
1 row created.
anbob@ANBOB>insert into t values (1,'a');
1 row created.
anbob@ANBOB>select * from t;
ID C
-------------------- ----------
1 a
2 a
1 a

anbob@ANBOB>select distinct id,typ_clob(c) new_c from t;
ID NEW_C(C)
-------------------- ----------
1 TYP_CLOB('
a')
2 TYP_CLOB('
a')
anbob@ANBOB>with v as (select distinct id,typ_clob(c) new_c from t)
2 select id,treat(new_c as typ_clob).c as c from v;
ID C
-------------------- ----------
1 a
2 a
复制

-- end --
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论