在SQL Server 2016中为列设置排序规则

时间:2017-10-04 20:00:11

标签: sql sql-server-2016

我正在测试以下代码进行整理。

declare @table table
               (
                    col1 varchar(10) collate Latin1_General_CS_AS 
                    -- modified for column
               )

insert into @table 
values('abcd')

declare @table1 table
                (
                    col2 varchar(10) 
                    --database default(collate SQL_Latin1_General_CP1_CI_AS)
                )

insert into @table1
values('abcd')

-- executing this results in an error 'Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CS_AS" in the equal to operation.'

select * 
from @table a 
join @table1 b on a.col1 = b.col2 

--executing below works fine
select * 
from @table a 
join @table1 b on a.col1 collate Latin1_General_CS_AS  = b.col2 

--executing below again throws error
select * 
from @table a 
join @table1 b on a.col1 collate Latin1_General_CS_AS  = b.col2 collate SQL_Latin1_General_CP1_CI_AS

需要理解,虽然我在表变量中指定了排序规则,但仍然会导致错误。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您只在比较的一侧指定default printer(无论哪一方都不重要)。

Application.ActivePrinter

您不能像在最后一个示例中那样指定两个不同排序规则的比较(... CS_AS& ... CI_AS)。

答案 1 :(得分:0)

基本原则是您必须对列进行整理才能进行正确的比较。

如果a.col1整理是Latin1_General_CS_AS那么 你可以使用其中一个

  • 关于a.col1 = b.col2整理Latin1_General_CS_AS

  • 关于a.col1整理Latin1_General_CS_AS = b.col2

  • a.col1整理SQL_Latin1_General_CP1_CI_AI = b.col2整理 SQL_Latin1_General_CP1_CI_AI(只要它是相同的,你可以改变两边)