简体   繁体   English

文字栏上的SQL INNER JOIN

[英]SQL INNER JOIN on Text Columns

I have two tables (equipment & software) that I want to do an INNER JOIN on. 我有两个要进行INNER JOIN表(设备和软件)。 They both have a field called EQCN. 他们都有一个名为EQCN的字段。 It is a text field. 它是一个文本字段。 I get the following error: 我收到以下错误:

The data types text and text are incompatible in the equal to operator. 数据类型text和text在等于运算符中不兼容。

There has to be a way around this. 必须有一种解决方法。

Change the data types for these columns to varchar(max) . 将这些列的数据类型更改为varchar(max)

From Microsoft : 来自微软

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. 在将来的Microsoft SQL Server版本中,将删除ntext,text和image数据类型。 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),varchar(max)和varbinary(max)。

Although this is odd, Microsoft suggests doing an indirect comparison on a text or ntext using something like SUBSTRING. 尽管这很奇怪,但Microsoft建议使用SUBSTRING之类的方法对text或ntext进行间接比较。 Example: 例:

SELECT * 
FROM t1 
JOIN t2 ON SUBSTRING(t1.textcolumn, 1, 20) = SUBSTRING(t2.textcolumn, 1, 20)

This of course raises a whole other set of issues like, what if the first # of characters are identical, etc. I would suggest going the route of changing the type if you can first, rather than take this advice. 当然,这会引起其他一系列问题,例如,如果第一个字符相同,该怎么办,等等。我建议您尽可能先改变字体的类型,而不是采纳此建议。

Source 资源

Doing a join on a TEXT field would be VERY slow, even if it did work. 即使确实起作用,在TEXT字段上进行联接也会很慢。 Perhaps use: 也许使用:

CONVERT(varchar, myColumnName) = 'my value'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM