0

如果我在托管数据库的远程 sql 服务器上运行相同的 t-sql,我有 t-sql,它在本地 sql 服务器上运行良好,导致错误

我尝试了很多更改,但对我没有任何效果,最后我决定在 Stack Exchange 上写作。

我用来自服务器的脚本创建了我的本地数据库,所以我希望它们不会改变任何数据类型,因为我的查询给了我正确的结果。

这是我的 t-sql

IF OBJECT_ID('tempdb..#TempAppliedData') IS NOT NULL  
  BEGIN  
        DROP TABLE #TempAppliedData  
  END  

Create Table #TempAppliedData(
--RecordNo [int],
IdNo1 [nvarchar](50) COLLATE DATABASE_DEFAULT,
--Name [nvarchar](50),
AYear2011 [char](3) COLLATE DATABASE_DEFAULT,
AYear2012 [char](3) COLLATE DATABASE_DEFAULT,
AYear20121 [char](3) COLLATE DATABASE_DEFAULT, 
AYear2013 [char](3) COLLATE DATABASE_DEFAULT,
AYear20131 [char](3) COLLATE DATABASE_DEFAULT
);


insert into #TempAppliedData (IdNo1) select distinct  IdNo from AppliedData


update #TempAppliedData 
set 
AYear2011=(select CASE WHEN COUNT(A.Year2011)=0 then 'No' else 'YES' end FROM AppliedData A WHERE Year2011='Yes' and IdNo1=IdNo) ;


update #TempAppliedData 
set 
AYear2012=(select CASE WHEN COUNT(A.Year2012)=0 then 'No' else 'YES' end FROM AppliedData A WHERE ApplnYear <=20131 AND Year2012='Yes' and IdNo1=IdNo)


update #TempAppliedData 
set 
AYear20121=(Select CASE WHEN COUNT(A.Year20121)=0 then 'No' else 'YES' end FROM AppliedData A WHERE ApplnYear <=20131 AND Year20121='Yes' and IdNo1=IdNo)



update #TempAppliedData 
set 
AYear2013=(Select CASE WHEN COUNT(A.Year2013)=0 then 'No' else 'YES' end FROM AppliedData A WHERE ApplnYear <=20131  AND Year2013='Yes' and IdNo1=IdNo)


update #TempAppliedData 
set 
AYear20131=(Select CASE WHEN COUNT(A.Year20131)=0 then 'No' else 'YES' end FROM AppliedData A WHERE ApplnYear <=20131  AND Year20131='Yes' and IdNo1=IdNo)




Select * from #TempAppliedData;

--select distinct IdNo from AppliedData

其产生错误如下:

Msg 468, Level 16, State 9, Line 28
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CS_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
Msg 468, Level 16, State 9, Line 40
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CS_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
Msg 468, Level 16, State 9, Line 53
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CS_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
Msg 468, Level 16, State 9, Line 66
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CS_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
Msg 468, Level 16, State 9, Line 79
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CS_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
4

3 回答 3

0

只需在有错误的地方添加后值 COLLATE SQL_Latin1_General_CP1_CS_AS

于 2013-10-31T10:09:47.390 回答
0

似乎您对 IdNo1 和 IdNo 列有不同的排序规则(您应该使用别名,以便人们知道列属于哪里)

update B 
set AYear2011 =
case when exists (
select 1
FROM AppliedData A 
WHERE A.Year2011='Yes' and B.IdNo1=A.IdNo collate SQL_Latin1_General_CP1_CI_AS) 
) then 'Yes' else 'No' End
-- or this: collate SQL_Latin1_General_CP1_CS_AS
from #TempAppliedData B

注意大的变化,否则 AYear2011 的值永远不会是“否”

于 2013-10-31T10:12:18.253 回答
0
Create Table #TempAppliedData(
 IdNo1 [nvarchar](50) COLLATE DATABASE_DEFAULT,

在这里,您正在创建具有托管默认排序规则的列tempdb。显然,您自己的数据库使用 CI 排序规则,而托管服务使用 CS 排序规则部署。

在脚本中使用显式排序规则,而不是依赖现有的部署排序规则。理想情况下,您应该始终在与部署相同的设置下进行开发。安优

于 2013-10-31T10:16:20.723 回答