sql – Union Causing ntext数据类型不能被选为DISTINCT,因为它不具有可比性

前端之家收集整理的这篇文章主要介绍了sql – Union Causing ntext数据类型不能被选为DISTINCT,因为它不具有可比性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个查询(由一些临时表组成),我想要联合在一起.当我用联盟注释掉一个(无关紧要的)查询时,它可以工作.但是当我介绍union时,我得到ntext数据类型不能被选为DISTINCT,因为它不具有可比性.
我不明白为什么,因为这两个主要查询都不使用.主要选择参考:
SELECT  
    dbo.CUSTTABLE.ACCOUNTNUM AS 'Account Number',dbo.CUSTTABLE.NAME AS 'Customer Name',[DESCRIPTION] AS 'Division',ISNULL(dbo.USERINFO.NAME,'OPEN') AS 'Sales Engineer',[STATE],PHONE,PAYMTERMID AS 'Terms',(CASE BLOCKED
        WHEN 0 THEN 'No'
        WHEN 2 THEN 'Financial Hold'
        WHEN 1 THEN 'System Hold'
        ELSE 'Unknown' END) AS 'Status',[45-60],[61-90],[91-120],[120+],#temptable2.Today,ISNULL(USERMEMO,'') AS 'Notes',ISNULL(#temptable2.Today / NULLIF((ISNULL(#salesTemp.PQ1,0) / 90),0),0) AS 'DSO1',CustTable.StatisticsGroup as 'Past Due Category'

FROM
    dbo.CUSTTABLE INNER JOIN 
    dbo.DIMENSIONS ON DIMENSION2_ = NUM INNER JOIN
    #temptable2 ON dbo.CUSTTABLE.ACCOUNTNUM = #temptable2.ACCOUNTNUM LEFT OUTER JOIN
    #temptable ON dbo.CUSTTABLE.ACCOUNTNUM = #temptable.ACCOUNTNUM LEFT OUTER JOIN
    dbo.SMMSALESUNITMEMBERS ON SALESGROUP = SALESUNITID AND SALESMANAGER = 1 LEFT OUTER JOIN
    dbo.USERINFO ON ID = SALESMANID AND [ENABLE] = 1    
    JOIN #salesTemp on #salesTemp.ACCOUNTNUM = CUSTTABLE.ACCOUNTNUM
WHERE
    DIMENSION2_ IN (@division)

UNION
SELECT 
    Customer_ID AS 'Account Number',Customer_Name AS 'Customer Name','South Bend' AS Division,'' AS 'Sales Engineer','Indiana' AS 'State','' AS 'Phone',Customer_Terms AS 'Terms','' AS 'Status',[Today],'' AS 'Notes','' AS 'DSO1','' AS 'Past Due Category'
FROM #temptable4

解决方法

更改:”AS’DSO1′,

NULL为’DS01′,

sql集中无法将空集转换为数字;这是根据联合的顶部选择中定义的数据类型所必需的.

我认为这是问题,因为联合中的顶部选择将返回一个数值.由于空集不能转换为数字,因此要么提供数字(0),要么使用null.

请记住,在执行联合时,列数必须匹配,并且它们的数据类型必须匹配.如果他们不这样做你会得到这样的错误.

至于评论,“我不明白为什么,因为两个主要查询都不使用不同的”A UNION对生成的联合结果执行不同的. UNION ALL不会.这就是为什么UNION ALL执行速度更快的原因,因为它不必执行UNION执行中包含的重复删除.

原文链接:https://www.f2er.com/mssql/78561.html

猜你在找的MsSQL相关文章