Oracle中对Clob字段进行Distinct的解决方法

oracle 10+g不支持对类型为CLOB的列进行distinct,也不支持union,所以在遇到此问题,需要对SQL语句进行重新,从另一思想去实现同样的效果的。union没仔细思考过,具体还要看union里面的条件如何,最简单的方法是利用to_char将clob字段转换成char型,但存在一个问题,如clob中的字符长度超过4000时会报错。在此主要对distinct的转换方法进行列举:

表结构如下:

Table1:

 id(varchar) title(varchar) content(clob)
 1 title1 <CLOB>
 2 title2 <CLOB>

Table2:

 id(varchar) table_id(varchar) object(varchar)
 1 1 123
 2 1 abc
 3 1 111
 4 1 222
 5 2 123

SQL语句:

select distinct table1.* from table2 table2 left join table1 table1 on table1.id = talbe2.table_id

where table2.object = '123' or table2.object = '111' or table2.object = '222' or table2.object = '333'

执行该语句,出现错误:ORA-00932,数据类型不一致:应为-,却获得CLOB。

 

1、利用rowid,过滤重复的数据。

这个方法的思路来源,就是对某个字段进行distinct时的解决方法。

基本思路:select   B.name,B.id   from   table   B   where     not   exists   (select   1   from   table   A     where   A.name   =   B.name   AND   B.id_key   >   A.id_key)

A表,B表为同一张表,表中的数据存在name相同的,但id_key不同,该语句的目的是把同名的只显示一条即可,其它同名记录过滤。

将该思想转换到当前的问题上,利用每条记录的rowid不同进行过滤。

select p4.*, p4.rowid from (
select p.* from table2 s left join table1 p on s.table_id = p.id
where s1.object = '123' or s1.object = '111' or s1.object = '222' or s1.object = '333'
) p4
where not exists (
select 1, p2.rowid from (
select p1.* from table2 s1 left join table1 p1 on s1.table_id= p1.id
where s1.object = '123' or s1.object = '111' or s1.object = '222' or s1.object = '333') p2
where p4.id = p2.id and p4.rowid > p2.rowid
)

2、采用子查询进行过滤重复的数据。

 select * from table1 t1 where t1.id in (

 selcet distinct p1.id from table2 s1 left join table1 p1 on s1.table_id= p1.id
      where s1.object = '123' or s1.object = '111' or s1.object = '222' or s1.object = '333'

)

3、采用to_char函数,直接使用distinct(前提是:clob字段中的字符长度少于4000)。

select distinct table1.id, table1.tittle, to_char(table1.content) from table2 table2 

        left join table1 table1 on table1.id =  talbe2.table_id

        where table2.object = '123' or table2.object = '111' or table2.object = '222' or table2.object = '333'

转载于:https://www.cnblogs.com/Csharpblogs/archive/2011/07/16/2107995.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 `exp` 命令导出 Oracle 数据库时,如果存在 CLOB 类型的字段,可能会出现报错的情况。这是因为 `exp` 命令默认使用 `varchar2` 类型来处理 CLOB 字段,而 `varchar2` 类型的长度有限制,无法处理过长的 CLOB 字段解决这个问题有两种方法: 1. 使用 `expdp` 命令导出数据:`expdp` 命令是 Oracle 数据库的数据泵工具,它可以更好地处理 CLOB 类型的数据。使用 `expdp` 命令导出数据时,需要指定 `CLOB` 类型的列使用二进制格式导出,例如: ``` expdp user/password@database tables=table_name directory=dir dumpfile=file_name.dmp lob_as_segment=y ``` 其,`lob_as_segment=y` 表示将 `CLOB` 类型的列以二进制格式导出。 2. 修改 `exp` 命令的参数:可以通过修改 `exp` 命令的参数来解决 CLOB 字段报错的问题。具体操作如下: 1. 在 `exp` 命令添加 `-c` 参数,表示以字符格式导出数据。 2. 在 `exp` 命令添加 `-lob` 参数,表示导出 CLOB 字段。 3. 修改 `NLS_LANG` 环境变量,将其设为 `AMERICAN_AMERICA.AL32UTF8`,表示使用 UTF-8 编码。 示例命令如下: ``` exp user/password@database tables=table_name file=file_name.dmp log=log_name.log rows=yes compress=no direct=no indexes=no triggers=no constraints=no grants=no feedback=1000000 buffer=1000000 consistent=y commit=y statistics=none object_consistent=y recordlength=65535 consistent=y full=y rows=y constraints=n indexes=n grants=n triggers=n feedback=1000000 buffer=1000000 file=file_name.dmp log=log_name.log compress=n consistent=y direct=n rows=y statistics=none file_size=unlimited consistent=y file_name_convert=old_dir:new_dir -c -lob -NLS_LANG=AMERICAN_AMERICA.AL32UTF8 ``` 注意,修改 `NLS_LANG` 环境变量可能会影响其他程序的运行,需要谨慎操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值