Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inconsistent datatypes: expected - got CLOB for table join

Tags:

join

oracle

clob

Oracle XE 11. a very simple join query gave me the following error:

ORA-00932: inconsistent datatypes: expected - got CLOB

Tables:

Product
----------------------------------
id, name, description, categoryId 


Catetory
------------------
id, name

The product description is CLOB.

SQL> desc Product;

 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                        NOT NULL NUMBER(19)
 NAME                                      NOT NULL VARCHAR2(30 CHAR)
 CATEGORYID                                         NUMBER(19)
 DESCRIPTION                                        CLOB

SQL> desc Category;

 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                        NOT NULL NUMBER(19)
 NAME                                      NOT NULL VARCHAR2(30 CHAR)

Query:

SELECT DISTINCT t1.ID, t1.DESCRIPTION, t1.NAME, t0.name FROM Product t1 
LEFT OUTER JOIN Category t0 ON (t0.ID = t1.categoryId);

ERROR at line 1: ORA-00932: inconsistent datatypes: expected - got CLOB

IF I remove the t0.name from selection, it will work. weird.

SELECT DISTINCT t1.ID, t1.DESCRIPTION, t1.NAME FROM Product t1 
LEFT OUTER JOIN Category t0 ON (t0.ID = t1.categoryId);

Thanks.

like image 663
Dave Avatar asked Jun 20 '16 21:06

Dave


People also ask

How do you use CLOB in Group by clause?

You cannot specify LOB columns in the ORDER BY clause of a query, or in the GROUP BY clause of a query or in an aggregate function. To get around this you can to_char the column if it's < 4000 characters and substr it if it's longer. "there is no reason for it to be present in the GROUP BY list."

What is CLOB datatype?

The CLOB data type stores any kind of text data in random-access chunks, called sbspaces. Text data can include text-formatting information, if this information is also textual, such as PostScript, Hypertext Markup Language (HTML), Standard Graphic Markup Language (SGML), or Extensible Markup Language (XML) data.

How do I fix Ora 00932?

To correct this error, you can do one of the following: Not use Oracle functions in your SQL (against the LONG datatype field). Consider modifying your table so that the supplier_name field is either a VARCHAR2 or CHAR field. Try writing a custom PLSQL function to convert a LONG to a VARCHAR2.

What is CLOB datatype in Oracle with example?

A CLOB (character large object) value can be up to 2,147,483,647 characters long. A CLOB is used to store unicode character-based data, such as large documents in any character set.


2 Answers

The DISTINCT keyword cannot be used for CLOB datatypes. The workaround is :

SELECT a.*
     , b.clob
 FROM  (SELECT DISTINCT
               ... /* columns list wihtout clob columns */
         FROM  ...
        ) a
 JOIN
       table_with_clobs b
  ON   ...

Going to your sample it would be:

SELECT Po.ID, Po.DESCRIPTION, Po.NAME, PC.CatName 
  FROM
   ( SELECT DISTINCT t1.ID, t0.name CatName 
       FROM Product t1 
       LEFT OUTER JOIN Category t0 
         ON t0.ID = t1.categoryId 
    ) PC
    join Product PO
    on PO.ID = PC.ID
like image 180
dcieslak Avatar answered Nov 01 '22 18:11

dcieslak


If your clob column does not contain more than 4000 characters, you could try this..

SELECT DISTINCT t1.ID, to_char(t1.DESCRIPTION), t1.NAME FROM Product t1 
LEFT OUTER JOIN Category t0 ON (t0.ID = t1.categoryId);
like image 45
cableload Avatar answered Nov 01 '22 17:11

cableload