【问题标题】:How to fix "The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator."如何解决“无法比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。”
【发布时间】:2016-12-23 00:06:15
【问题描述】:

这是我目前所拥有的:

SELECT 
    account, ' ', message_type, 
    MAX(message_no) as max_message_no, message_text
FROM
    (SELECT 
         account, message_type, message_no, message_text
     FROM  
         messages m 
     INNER JOIN 
         receivables r ON m.account = r.aracct
     WHERE 
         m.account IN (SELECT r.aracct AS account 
                       FROM receivables r 
                       WHERE r.balance <> 0)
         AND m.message_type = 'N') t1
GROUP BY 
    t1.account, t1.message_type, t1.message_text

我不断收到错误消息:

text、ntext 和 image 数据类型不能进行比较或排序,除非使用 IS NULL 或 LIKE 运算符。

我基本上想要每个帐户的最大 message_no,我希望它显示 message_text,但它不允许它,因为它是 text 数据类型。

【问题讨论】:

  • 停止使用 textntextimage 数据类型,因为十多年来 MS 一直在警告它们将被删除?
  • 不是我的电话,去和我的老板谈谈。多年来我一直这么说
  • 好吧,那就告诉你的老板,只要数据类型保持为text,你就无法完成查询......

标签: sql-server sql-server-2008 tsql


【解决方案1】:

TEXTNTEXTIMAGE 已弃用,应替换为相应的类型 VARCHAR(MAX)NVARCHAR(MAX)VARBINARY(MAX)。您可以使用

投射您的 TEXT-column
CAST(t1.message_text AS VARCHAR(MAX))

在您的子选择中执行一次可能就足够了:

SELECT account, ' ', message_type, MAX(message_no) as max_message_no, message_text
FROM
(
SELECT account, message_type, message_no, CAST(message_text AS VARCHAR(MAX)) AS message_text
    FROM messages m INNER JOIN receivables r ON m.account = r.aracct
        WHERE m.account IN (SELECT r.aracct AS account FROM receivables r WHERE r.balance <> 0)
        AND m.message_type = 'N'
) t1
GROUP BY t1.account, t1.message_type, t1.message_text

【讨论】:

    【解决方案2】:

    不能比较Text、Image、数据类型..如果要使用,请将它们更改为Varchar或Nvarchar..

    下面的一些例子

       create table #t
        (
        yy text
        )
    
        insert into #t
        select '1'
    
    
        --fails
        select * from #t 
        where yy='1'
        /****
        Msg 402, Level 16, State 1, Line 1
        The data types text and varchar are incompatible in the equal to operator.**/
    
        --fails
        select yy from #t
        group by yy
    
        /****
        Msg 306, Level 16, State 2, Line 1
        The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
        ***/
    
    
        ---Workaround 
        --Try changing the datatypes involved to Varchar or Nvarchar
        select * from #t 
        where cast(yy as varchar(5))=1
    
    
        select cast(yy as varchar(10)) from #t 
        group by cast(yy as varchar(10))
    

    参考资料:
    The text, ntext, and image data > types cannot be compared or sorted, except when using IS NULL or LIKE > operator

    【讨论】:

    • 顺便说一句:: 将TEXT 转换为VARCHAR(MAX)NTEXT 转换为NVARCHAR(MAX) 似乎相当明显。我是NVARCHAR 的情况,您应该将文字设置为前导N'1'...
    【解决方案3】:

    所以您的查询的问题是您在主要部分中将其转换为

    SELECT account, ' ', message_type, MAX(message_no) as max_message_no, message_text
    FROM
    (
    SELECT account, message_type, message_no, CAST(message_text AS VARCHAR(MAX)) AS message_text
        FROM messages m INNER JOIN receivables r ON m.account = r.aracct
            WHERE m.account IN (SELECT r.aracct AS account FROM receivables r WHERE r.balance <> 0)
            AND m.message_type = 'N'
    ) t1
    GROUP BY t1.account, t1.message_type, CAST(message_text AS VARCHAR)
    

    【讨论】:

      猜你喜欢
      • 2013-02-05
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      相关资源
      最近更新 更多