ntext与int SQL Server不兼容

时间:2014-02-07 22:32:48

标签: sql-server

当我收到错误时,我正在处理sql:我不知道如何修复它:

ntext is incompatible with int

我的查询就像这样

select * from table1 where id=textfield union select name,age,(here is an ntext value) from table2

我尝试使用convert(myfield as nvarchar(max)),但仍然没有运气,错误也改为:

The ntext data type cannot be selected as DISTINCT because it is not comparable.

1 个答案:

答案 0 :(得分:1)

第一:

ntext已弃用,应替换为nvarchar(MAX)

除此之外,ntext无法比较,无法与DISTINCT=一起使用。

当您使用关键字UNION加入2个查询时,它会自动调用DISTINCT操作,以仅加入来自两个查询的唯一数据。

但是,nvarchar(MAX)可以在DISTINCT内使用,因此,最佳解决方案是更新模型以使用此类型而不是ntext

如果无法做到这一点,请执行select * from table1而不是:

select field1, field2, Cast(field3 as NVarchar(Max)) 
from table1 
union 
select field1, field2, Cast(field3 as nvarchar(MAX))
from table2

最后一点:

建议在使用union而不是*通配符时显式调用字段。