最近维护一个系统,在一个存储过程中涉及到对多个数据库的操作,然后遇到这么一个错误‘equal to 操作的排序规则冲突’,费了好大劲才把他弄好,原来还真是不同表中字段的排序规则不同造成的。解决方法就是在字段后面加上 COLLATE +排序规则(比如COLLATE Chinese_PRC_CI_AS),以使涉及到的不同表的字段排序规则一致。
贴上一段SQL脚本,以备今后参考。

Code
alter proc GetChargeJob
(
@office varchar(20),
@dept varchar(20),
@userName varchar(50)
)
as
declare @chargerOffice varchar(20)
--获取负责人所在Office
select @chargerOffice=office COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where email COLLATE Chinese_PRC_CI_AS =@userName
if @dept='Research'
begin
SELECT B.JobNo AS JobNo, B.JobWave AS JobWave, B.JobType AS JobType, A.SPRID+A.CONTRACTID AS CTRID,A.*
FROM mContract A LEFT JOIN dJobInfo B ON A.JID = B.JID
WHERE B.OFFICE=@OFFICE
ORDER BY B.AddDate DESC
end
else
begin
if @dept='FW'
begin
select i.JobNo as JobNo,i.JobWave as JobWave,i.JobType as JobType,c.SprID+c.ContractID as CTRID,c.*,i.AddDate
from mContract c,dJobDuty d,dJobInfo i
where c.jid =d.jid
and i.jid=d.jid
and c.office=d.office
and d.FW in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice)
union select i.JobNo as JobNo,i.JobWave as JobWave,i.JobType as JobType,c.SprID+c.ContractID as CTRID,c.*,i.AddDate
from dJobDuty d left join mContract c on d.JID=c.JID
left join dJobInfo i on i.JID=c.JID
where d.FW in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice) and d.Office not in( select d.Office
from mContract c,dJobDuty d,dJobInfo i
where c.jid=d.jid
and i.jid=d.jid
and c.office=d.office
and d.FW in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice))
order by i.AddDate desc
end
if @dept='QC'
begin
select i.JobNo as JobNo,i.JobWave as JobWave,i.JobType as JobType,c.SprID+c.ContractID as CTRID,c.*,i.AddDate
from mContract c,dJobDuty d,dJobInfo i
where
c.jid=d.jid
and i.jid=d.jid
and c.office=d.office
and d.QC in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice)
union select i.JobNo as JobNo,i.JobWave as JobWave,i.JobType as JobType,c.SprID+c.ContractID as CTRID,c.*,i.AddDate
from dJobDuty d left join mContract c on d.JID=c.JID
left join dJobInfo i on i.JID=c.JID
where d.QC in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice) and d.Office not in( select d.Office
from mContract c,dJobDuty d,dJobInfo i
where c.jid=d.jid
and i.jid=d.jid
and c.office=d.office
and d.QC in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice)
)
order by i.AddDate desc
end
if @dept='E&C'
begin
select i.JobNo as JobNo,i.JobWave as JobWave,i.JobType as JobType,c.SprID+c.ContractID as CTRID,c.*,i.AddDate
from mContract c,dJobDuty d,dJobInfo i
where
c.jid=d.jid
and i.jid=d.jid
and c.office=d.office
and d.EC in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice)
union select i.JobNo as JobNo,i.JobWave as JobWave,i.JobType as JobType,c.SprID+c.ContractID as CTRID,c.*,i.AddDate
from dJobDuty d left join mContract c on d.JID=c.JID
left join dJobInfo i on i.JID=c.JID
where d.EC in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice) and d.Office not in( select d.Office
from mContract c,dJobDuty d,dJobInfo i
where c.jid=d.jid
and i.jid=d.jid
and c.office=d.office
and d.EC in (select email COLLATE Chinese_PRC_CI_AS from SmartGuys.dbo.Portal_Users where office COLLATE Chinese_PRC_CI_AS=@chargerOffice)
)
order by i.AddDate desc
end
end

G