我有以下SQL查询:
SELECT DISTINCT
prod_no,
prod_text,
RTRIM (
XMLAGG (XMLELEMENT (e, prod_desc, ',').EXTRACT (
'//text()') ORDER BY prod_desc).getclobval (),
',')
FROM mytable
WHERE prod_no = 'XCY'
GROUP BY prod_no,
prod_text
当我执行时,我正在
ORA-00932:不一致的数据类型:预期 - 获得CLOB
更新1
DDL和样本数据
CREATE TABLE mytable
(
prod_no VARCHAR2 (30 BYTE) NOT NULL,
prod_text VARCHAR2 (30 BYTE) NOT NULL,
prod_desc CLOB
);
SET DEFINE OFF;
INSERT INTO mytable (prod_no, prod_text, prod_desc)
VALUES ('XCY', 'DECKS', 'THIS IS TEST');
INSERT INTO mytable (prod_no, prod_text, prod_desc)
VALUES ('ABC', 'DECKS', 'THIS IS TEST 2');
COMMIT;
答案 0 :(得分:4)
问题在于DISTINCT
和ORDER BY
。 Oracle不允许在CLOB上执行这些操作。您使用的是group by
,因此无论如何您都不需要DISTINCT
。
如果您不介意描述顺序,则以下内容将有效。
SELECT
prod_no,
prod_text,
RTRIM (
XMLAGG (XMLELEMENT (e, prod_desc, ',') ).EXTRACT (
'//text()').getclobval (),
',')
FROM mytable
WHERE prod_no = 'XCY'
GROUP BY prod_no,
prod_text;
如果您必须通过它进行排序,则可以将CLOB
转换为varchar2
并按顺序排序:
SELECT
prod_no,
prod_text,
RTRIM (
XMLAGG (XMLELEMENT (e, prod_desc, ',')
ORDER BY cast(prod_desc as varchar2(4000))).EXTRACT (
'//text()').getclobval (),
',')
FROM mytable
WHERE prod_no = 'XCY'
GROUP BY prod_no,
prod_text