从TABLE中选择DISTINCT CLOB_COLUMN;

时间:2010-10-13 08:37:50

标签: oracle plsql ora-00932

我想找到可以假定名为COPIA的表中包含名为CLOB_COLUMN(类型为CLOB)的列的不同CLOB值。

我已经选择了一个PROCEDURAL方法来解决这个问题,但我更愿意给出一个简单的SELECT,如下所示:SELECT DISTINCT CLOB_COLUMN FROM TABLE避免错误“ORA-00932:不一致的数据类型:预期 - 得到了CLOB”

我怎样才能做到这一点?

提前感谢您的友好合作。这是我想到的程序方式:

-- Find the distinct CLOB values that can assume the column called CLOB_COLUMN (of type CLOB)
-- contained in the table called COPIA
-- Before the execution of the following PL/SQL script, the CLOB values (including duplicates) 
-- are contained in the source table, called S1
-- At the end of the excecution of the PL/SQL script, the distinct values of the column called CLOB_COLUMN
-- can be find in the target table called S2

BEGIN
   EXECUTE IMMEDIATE 'TRUNCATE TABLE S1 DROP STORAGE';

   EXECUTE IMMEDIATE 'DROP TABLE S1 CASCADE CONSTRAINTS PURGE';
EXCEPTION
   WHEN OTHERS
   THEN
      BEGIN
         NULL;
      END;
END;

BEGIN
   EXECUTE IMMEDIATE 'TRUNCATE TABLE S2 DROP STORAGE';

   EXECUTE IMMEDIATE 'DROP TABLE S2 CASCADE CONSTRAINTS PURGE';
EXCEPTION
   WHEN OTHERS
   THEN
      BEGIN
         NULL;
      END;
END;

CREATE GLOBAL TEMPORARY TABLE S1
ON COMMIT PRESERVE ROWS
AS
   SELECT CLOB_COLUMN FROM COPIA;

CREATE GLOBAL TEMPORARY TABLE S2
ON COMMIT PRESERVE ROWS
AS
   SELECT *
     FROM S1
    WHERE 3 = 9;

BEGIN
   DECLARE
      CONTEGGIO   NUMBER;

      CURSOR C1
      IS
         SELECT CLOB_COLUMN FROM S1;

      C1_REC      C1%ROWTYPE;
   BEGIN
      FOR C1_REC IN C1
      LOOP
         -- How many records, in S2 table, are equal to c1_rec.clob_column?
         SELECT COUNT (*)
           INTO CONTEGGIO
           FROM S2 BETA
          WHERE DBMS_LOB.
                 COMPARE (BETA.CLOB_COLUMN,
                          C1_REC.CLOB_COLUMN) = 0;

         -- If it does not exist, in S2, a record equal to c1_rec.clob_column, 
         -- insert c1_rec.clob_column in the table called S2
         IF CONTEGGIO = 0
         THEN
            BEGIN
               INSERT INTO S2
                    VALUES (C1_REC.CLOB_COLUMN);

               COMMIT;
            END;
         END IF;
      END LOOP;
   END;
END;

7 个答案:

答案 0 :(得分:7)

您可以比较CLOB的哈希值以确定它们是否不同:

SELECT your_clob
  FROM your_table
 WHERE ROWID IN (SELECT MIN(ROWID) 
                   FROM your_table
                  GROUP BY dbms_crypto.HASH(your_clob, dbms_crypto.HASH_SH1))

编辑:

HASH函数不保证不会发生冲突。但是,根据设计,你不可能发生任何碰撞。但是,如果碰撞风险(< 2 ^ 80?)不可接受,您可以通过比较(dbms_lob.compare)具有相同哈希值的行的子集来改进查询。

答案 1 :(得分:4)

使用此方法。在表格个人资料内容是NCLOB。我添加了where子句来减少运行所需的时间,这是高的,

with
  r as (select rownum i, content from profile where package = 'intl'),
  s as (select distinct (select min(i) from r where dbms_lob.compare(r.content, t.content) = 0) min_i from profile t where t.package = 'intl')
select (select content from r where r.i = s.min_i) content from s
;

它不是为了赢得任何效率奖,而是应该有效。

答案 2 :(得分:4)

如果可以将字段截断为32767个字符,则可以使用:

select distinct dbms_lob.substr(FIELD_CLOB,32767) from Table1

答案 3 :(得分:1)

从table_name中选择不同的DBMS_LOB.substr(column_name,3000);

答案 4 :(得分:1)

如果将Clob截断为varchar2的大小不起作用,并且您担心哈希冲突,则可以:

  • 在每行中添加一个行号;
  • 在不存在的子查询中使用DBMS_lob.compare。排除行数较高的重复项(这意味着:compare = 0)。

例如:

create table t (
  c1 clob
);

insert into t values ( 'xxx' );
insert into t values ( 'xxx' );
insert into t values ( 'yyy' );

commit;

with rws as (
   select row_number () over ( order by rowid ) rn,
          t.*
   from   t
)
  select c1 from rws r1
  where  not exists (
    select * from rws r2
    where  dbms_lob.compare ( r1.c1, r2.c1 ) = 0
    and    r1.rn > r2.rn
  );

C1    
xxx   
yyy  

答案 5 :(得分:1)

在不同关键字之后添加TO_CHAR,以将CLOB转换为CHAR

SELECT DISTINCT TO_CHAR(CLOB_FIELD) from table1;   //This will return distinct values in CLOB_FIELD

答案 6 :(得分:0)

要绕过oracle错误,你必须做这样的事情:

从表COPIA C1中选择CLOB_COLUMN 在哪里C1.ID IN(从COPIA C2中选择DISTINCT C2.ID ......)