从其他数据库上的表更新时-无法解决排序规则冲突

时间:2019-02-15 10:37:22

标签: sql-server

我正在尝试根据另一个数据库中另一个表的条件来更新表,如下所示:

( IFS=- ; printf '%s\n' "${final[*]}" ; )

但是会引发错误:

  

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

所以我尝试了以下操作:

 update Table1
    set Table1.Name=[release].[People].[Details].Name
    from Table1  inner join [release].[People].[Details]  on Table1.AccountNumber =[release].[People].[Details].AccountNumber 

我也尝试过:

update Table1
    set Table1.Name=[release].[People].[Details].Name
    from Table1  inner join [release].[People].[Details]  on Table1.AccountNumber =[release].[People].[Details].AccountNumber 
where   Table1.AccountNumber COLLATE DATABASE_DEFAULT =[release].[People].[Details].AccountNumber  COLLATE DATABASE_DEFAULT

,它们不起作用。我无法更改表结构。

5 个答案:

答案 0 :(得分:0)

您正在尝试比较联接的ON子句中来自两个不同归类的两列:

  • Latin1_General_CI_AS
  • SQL_Latin1_General_CP1_CI_AS

作为一种解决方法,您可以尝试将一列转换为另一列的排序规则:

UPDATE t1
SET Name = d.Name
FROM Table1 t1
INNER JOIN [release].[People].[Details] d
    ON t1.AccountNumber COLLATE SQL_Latin1_General_CP1_CI_AS =
       d.AccountNumber COLLATE SQL_Latin1_General_CP1_CI_AS;

答案 1 :(得分:0)

尝试一次这样使用

update Table1
    set Table1.Name=[release].[People].[Details].Name
    from Table1  inner join [release].[People].[Details]  on Table1.AccountNumber COLLATE Latin1_General_CI_AS =[release].[People].[Details].AccountNumber COLLATE  Latin1_General_CI_AS 

或通过使用以下查询更改cloumn

ALTER TABLE Table1
  ALTER COLUMN AccountNumber 
    int COLLATE Latin1_General_CI_AS NOT NULL

答案 2 :(得分:0)

排序规则与JOIN有关。请尝试

   update Table1
set Table1.Name=[release].[People].[Details].Name
from Table1  inner join [release].[People].[Details]  
     on Table1.AccountNumber = [release].[People].[Details].AccountNumber 
      COLLATE 'Latin1_General_CI_AS' 

有时您需要使用这样的别名

 update Table1
 set Table1.Name=rel.Name
 from Table1  
  inner join [release].[People].[Details] rel 
      on Table1.AccountNumberCOLLATE 'Latin1_General_CI_AS'  =
         rel.AccountNumber COLLATE 'Latin1_General_CI_AS' 

答案 3 :(得分:0)

排序规则链接到字符串数据类型,并定义如何将它们与其他字符串进行比较。例如Latin1_General_CI_AS,后4个字符表示

  • CI:不区分大小写。可能是CS代表敏感。
  • AS:对上升敏感。可能是我不敏感吗?

检查以下示例:

  • 使用不敏感上升 返回{}和Cafe

    Café
  • 上升敏感 不返回结果

    DECLARE @Text1 VARCHAR(100) = 'Café' -- has ascent
    DECLARE @Text2 VARCHAR(100) = 'Cafe'
    
    SELECT 
        'match'
    WHERE 
        @Text1 COLLATE Latin1_General_CI_AI = 
        @Text2 COLLATE Latin1_General_CI_AI
    
  • 使用不区分大小写 会匹配(将低位字符与高位字符进行比较)

    DECLARE @Text1 VARCHAR(100) = 'Café' -- has ascent
    DECLARE @Text2 VARCHAR(100) = 'Cafe'
    
    SELECT 
        'no match'
    WHERE 
        @Text1 COLLATE Latin1_General_CI_AS = 
        @Text2 COLLATE Latin1_General_CI_AS
    
  • 归类还会影响记录的分组方式(因为它们与其他行进行了比较):

    DECLARE @Text1 VARCHAR(100) = 'STRONG weak'
    DECLARE @Text2 VARCHAR(100) = 'strong WEAK'
    
    SELECT 
        'match'
    WHERE 
        @Text1 COLLATE Latin1_General_CI_AI = 
        @Text2 COLLATE Latin1_General_CI_AI
    
  • 但是,如果比较的两边之间的排序规则不匹配,则引擎将不会冒任何风险,因此会引发错误:

    DECLARE @Table TABLE (String VARCHAR(100))
    INSERT INTO @Table (String) 
    VALUES ('Café'), ('Cafe') -- One with ascent, another without
    
    SELECT 
        String = T.String COLLATE Latin1_General_CI_AI,
        Rows = COUNT(1)
    FROM
        @Table AS T
    GROUP BY
        T.String COLLATE Latin1_General_CI_AI -- Ascent insensitive!
    
    /*
    Results:
        String  Rows
        Café    2
    */
    
  

468消息,级别16,状态9,第7行无法解决排序规则   在“ Latin1_General_CS_AI”和“ Latin1_General_CI_AI”之间存在冲突   等于操作。

在错误中,您比较了不同的字符集(不是大小写或上升,而是可用的语言字符):DECLARE @Text1 VARCHAR(100) = 'STRONG weak' DECLARE @Text2 VARCHAR(100) = 'strong WEAK' SELECT 'error!' WHERE @Text1 COLLATE Latin1_General_CI_AI = @Text2 COLLATE Latin1_General_CS_AI -- Different collation 与。 Latin1_General_CI_AS

您可以通过显式强制转换其中一个匹配另一个来解决此问题。我不知道是哪一个,因此以下解决方案会将两者都转换为同一个:

SQL_Latin1_General_CP1_CI_AS

答案 4 :(得分:0)

将COLLATE DATABASE_DEFAULT应用于在线子句

update Table1
    set Table1.Name=[release].[People].[Details].Name
    from Table1 
    join [release].[People].[Details]  
        on Table1.AccountNumber = [release].[People].[Details].AccountNumber  COLLATE DATABASE_DEFAULT