63

我知道,我知道我在问题中写的内容我不应该感到惊讶。但是我的情况正在慢慢地在继承的 POS 系统上运行,而我的前任显然不知道 JOIN,所以当我查看加载 60 秒的内部页面之一时,我发现它相当快,将这 8 个查询重写为一个查询 JOINs 情况。问题是,除了不知道 JOIN 之外,他似乎还对多个数据库有一种迷恋,而且令人惊讶的是,他们使用不同的排序规则。事实上,我们使用所有“正常”的拉丁字符,说英语的人会考虑整个字母表,而这整个东西将在几个月内停止使用,所以我只需要一个创可贴。

长话短说,我需要某种方法来转换为单个排序规则,这样我就可以比较两个数据库中的两个字段。

确切的错误是:

无法解决等于操作中“SQL_Latin1_General_CP850_CI_AI”和“SQL_Latin1_General_CP1_CI_AS”之间的排序规则冲突。

4

2 回答 2

129

您可以在查询中使用 collat​​e 子句(我现在找不到我的示例,所以我的语法可能是错误的 - 我希望它为您指明了正确的方向)

select sone_field collate SQL_Latin1_General_CP850_CI_AI
  from table_1
    inner join table_2
      on (table_1.field collate SQL_Latin1_General_CP850_CI_AI = table_2.field)
  where whatever
于 2010-02-18T17:39:45.860 回答
65

A general purpose way is to coerce the collation to DATABASE_DEFAULT. This removes hardcoding the collation name which could change.

It's also useful for temp table and table variables, and where you may not know the server collation (eg you are a vendor placing your system on the customer's server)

select
    sone_field collate DATABASE_DEFAULT
from
    table_1
    inner join
    table_2 on table_1.field collate DATABASE_DEFAULT = table_2.field
where whatever
于 2010-02-18T18:26:20.290 回答