【问题标题】:Extract a value from another table if column is null in Oracle如果 Oracle 中的列为空,则从另一个表中提取一个值
【发布时间】:2014-10-05 05:01:43
【问题描述】:

我有一个表格(table1),格式如下

FN               DESC_TXT       ST_CD
123              Text1             Q
124              (null)            Q
125              Text3             Q
126              Text4             P
127              (null)            Q

我想在 ST_CD='Q' 时提取记录,然后如果 DESC_TXT 为 (null) - 我需要检查另一个表 (table2) 并获取 Desc。另一个表结构是

FN     C_DESC_TXT       
124     Text2
127     Text5

所以我的输出是

FN               DESCRIPTION       
123              Text1             
124              Text2          
125              Text3         
127              Text5

我下面的代码是:

SELECT T1.FN, COALESCE(T1.DESC_TXT, T2.C_DESC_TXT) AS DESCRIPTION
FROM table T1
LEFT JOIN table2 T2
ON T1.FN = T2.FN
where T1.ST_CD= 'Q'

但是我得到了错误 - ORA-00932: inconsistent datatypes: expected NCHAR got NCLOB

因为我的DESC_TXT 是 NVARCHAR2 而C_DESC_TXT 是 NCLOB。

当我尝试NVL

SELECT T1.FN, NVL(T1.DESC_TXT, T2.C_DESC_TXT) AS DESCRIPTION
FROM table T1
LEFT JOIN table2 T2
ON T1.FN = T2.FN
where T1.ST_CD= 'Q'

我得到以下错误:

ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 2506, maximum: 2000)
22835. 00000 -  "Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: %s, maximum: %s)"
*Cause:    An attempt was made to convert CLOB to CHAR or BLOB to RAW, where
           the LOB size was bigger than the buffer limit for CHAR and RAW
           types.
           Note that widths are reported in characters if character length
           semantics are in effect for the column, otherwise widths are
           reported in bytes.
*Action:   Do one of the following
           1. Make the LOB smaller before performing the conversion,
           for example, by using SUBSTR on CLOB
           2. Use DBMS_LOB.SUBSTR to convert CLOB to CHAR or BLOB to RAW.

还有其他方法可以获得所需的输出吗?

【问题讨论】:

    标签: oracle oracle11g coalesce


    【解决方案1】:

    你可以使用NVL:

    SELECT T1.FN, NVL(T1.DESC_TXT, T2.C_DESC_TXT) AS DESCRIPTION
    FROM table T1
    LEFT JOIN table2 T2
    ON T1.FN = T2.FN
    where T1.ST_CD= 'Q'
    

    另一种选择是使用DBMS_LOB.SUBSTR 将 NCLOB 转换为 VARCHAR2:

    SELECT T1.FN, NVL(T1.DESC_TXT, DBMS_LOB.SUBSTR(T2.C_DESC_TXT, 4000, 1)) AS DESCRIPTION
    FROM table T1
    LEFT JOIN table2 T2
    ON T1.FN = T2.FN
    where T1.ST_CD= 'Q'
    

    第三种选择是使用DECODE:

    SELECT T1.FN, DECODE(T1.DESC_TXT, NULL, T2.C_DESC_TXT, T1.DESC_TXT) AS DESCRIPTION
    FROM table T1
    LEFT JOIN table2 T2
    ON T1.FN = T2.FN
    where T1.ST_CD= 'Q'
    

    【讨论】:

    • 第二个代码有效,但我想要整个描述列,因为我必须根据描述进行文本分析,我可以在dbms_lob.substr中使用的最大长度是多少@
    • @RUser 可以使用的最大长度是 VARCHAR2 的长度(4000 字节)
    • @RUser 另一种选择是反过来将 VARCHAR2 转换为 CLOB:TO_CLOB(T1.DESC_TXT)
    • DECODE 可以很好地提取整个 Description ,因为我不确定输入列的长度以及要设置的值。那么我可以使用 DECODE 来达到这个目的吗?
    猜你喜欢
    • 2019-05-25
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    相关资源
    最近更新 更多