数据类型 text 不能用作 UNION、INTERSECT 或 EXCEPT 运算符的操作数,因为它不可比较 [英] The data type text cannot be used as an operand to the UNION, INTERSECT or EXCEPT operators because it is not comparable

查看:40
本文介绍了数据类型 text 不能用作 UNION、INTERSECT 或 EXCEPT 运算符的操作数,因为它不可比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子

  • ID (PK)
  • 所有者信息
  • DescriptionText 文本

连接到另一个表

  • ID (FK)
  • 参与者信息

所有者可以是参与者,如果是,则所有者和参与者中有相同的引用(进入用户表).所以我做到了:

The Owner can be a Participant, and if it is, the same reference (into user table) is in Owner and Participant. So I did:

SELECT TableA.Id,TableA.Owner,TableA.Text
FROM TableA
WHERE TableA.Owner=@User
UNION
SELECT TableA.Id,TableA.Owner.TableA.Text
FROM TableA LEFT JOIN TableB ON (TableA.Id=TableB.Id)
WHERE TableB.Participant = @User

此查询应返回所有不同的数据集,其中某个@User 是所有者或参与者或两者.

This query should return all distinct data sets where a certain @User is either Owner or Participant or both.

它会,如果 SQL Server 不抛出

And it would, if SQL Server wouldn't throw

数据类型文本不能用作 UNION、INTERSECT 或 EXCEPT 运算符的操作数,因为它不可比较.

The data type text cannot be used as an operand to the UNION, INTERSECT or EXCEPT operators because it is not comparable.

既然 Id 是一个 PK,而 Text 来自同一个表,为什么 SQL Server 还要比较 Text?

Since Id is a PK, and Text is from the same table, why would SQL Server want to compare Text at all?

我可以使用 UNION ALL 来停止重复检测,但我可以在不丢失结果的独特性的情况下规避它吗?

I can use UNION ALL to stop duplicate detection, but can I circumvent this without losing the distinctness of the results?

推荐答案

正确方法

停止使用 TEXT 它已过时.更改表架构.

Stop using TEXT it is obsolete. Alter table schema.

ntext、text 和 image 数据类型将在未来版本中删除的 Microsoft SQL Server.避免在 new 中使用这些数据类型开发工作,并计划修改当前使用的应用程序他们.改用 nvarchar(max)、varchar(max) 和 varbinary(max).

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

解决方法

转换为NVARCHAR(MAX):

SELECT TableA.Id,TableA.Owner, CAST(TableA.DescriptionText AS NVARCHAR(MAX))
FROM TableA
WHERE TableA.Owner=@User
UNION
SELECT TableA.Id,TableA.Owner, CAST(TableA.DescriptionText AS NVARCHAR(MAX))
FROM TableA LEFT JOIN TableB ON (TableA.Id=TableB.Id)
WHERE TableB.Participant = @User

这篇关于数据类型 text 不能用作 UNION、INTERSECT 或 EXCEPT 运算符的操作数,因为它不可比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆