ORA-00932:不一致的数据类型:预期 - 在UNOIN中得到CLOB

时间:2017-11-16 14:05:37

标签: oracle oracle12c

我的查询是2个查询的UNION,但即使我在两个查询中都使用相同的列,当我执行它时出现错误

   SELECT  username,
                sid,
                serial# serial,
                osuser,
                seconds_in_wait,
                sql_address,
                sql_hash_value,
                sql_fulltext
        FROM V$SESSION se
        JOIN v$sql sq ON sq.ADDRESS = se.SQL_ADDRESS
        AND sq.HASH_VALUE = se.SQL_HASH_VALUE
        WHERE status = 'ACTIVE'
            AND username <> 'SYSTEM' AND username <> 'SYS'
            AND osuser <> 'NT AUTHORITY\SYSTEM'
            AND osuser <> 'SYSTEM'
            AND seconds_in_wait >= 500

 UNION 
 SELECT  username,
                sid,
                serial# serial,
                osuser,
                seconds_in_wait,
                sql_address,
                sql_hash_value,
                sql_fulltext
from V$SESSION se 
    join v$sql sq on sq.ADDRESS = se.SQL_ADDRESS and sq.HASH_VALUE = se.SQL_HASH_VALUE
where blocking_session is not NULL 
AND seconds_in_wait >= 500

2 个答案:

答案 0 :(得分:0)

Oracle不支持UNION CLOB类型的列(在您的案例中为sql_fulltext)。

如果UNION ALL(而不是UNION)也适用于您的查询,则可以使用它。

<强>更新

您可能需要稍微修改您的查询以避免与UNION ALL重复(请参阅第一个WHERE子句中的最后一个布尔表达式):

SELECT username,
        sid,
        serial# serial,
        osuser,
        seconds_in_wait,
        sql_address,
        sql_hash_value,
        sql_fulltext
    FROM V$SESSION se
    JOIN v$sql sq ON sq.ADDRESS = se.SQL_ADDRESS
        AND sq.HASH_VALUE = se.SQL_HASH_VALUE
    WHERE status = 'ACTIVE'
        AND username <> 'SYSTEM' AND username <> 'SYS'
        AND osuser <> 'NT AUTHORITY\SYSTEM'
        AND osuser <> 'SYSTEM'
        AND seconds_in_wait >= 500
        AND blocking_session is NULL 
UNION ALL
SELECT username,
        sid,
        serial# serial,
        osuser,
        seconds_in_wait,
        sql_address,
        sql_hash_value,
        sql_fulltext
FROM V$SESSION se 
JOIN v$sql sq ON sq.ADDRESS = se.SQL_ADDRESS
    AND sq.HASH_VALUE = se.SQL_HASH_VALUE
WHERE blocking_session is not NULL 
    AND seconds_in_wait >= 500

答案 1 :(得分:0)

为什么你需要一个UNION开头?您的查询等同于:

   SELECT  username,
                sid,
                serial# serial,
                osuser,
                seconds_in_wait,
                sql_address,
                sql_hash_value,
                sql_fulltext
        FROM V$SESSION se
        JOIN v$sql sq ON sq.ADDRESS = se.SQL_ADDRESS
        AND sq.HASH_VALUE = se.SQL_HASH_VALUE
        WHERE seconds_in_wait >= 500
          AND (     status   =      'ACTIVE'
                AND username not in ('SYSTEM', 'SYS')
                AND osuser   not in ('NT AUTHORITY\SYSTEM', 'SYSTEM')
                OR
                blocking_session is not null
              )
;

然后:如果在另一个问题中你不能避免UNION(而不是UNION ALL),那么正确的做法就是忽略CLOB列。第一;选择所有其他列,执行连接和UNION(请注意JOINUNION分配的,因此最好先执行UNION只有JOIN),然后JOIN再次返回以检索CLOB列。