How to fix a collation conflict in a SQL Server query?

后端 未结 5 1964
故里飘歌
故里飘歌 2020-12-09 00:53

I am working on a view, wherein I am using an inner join on two tables which are from two different servers. We are using linked server. When running the query I am getting

5条回答
  •  被撕碎了的回忆
    2020-12-09 01:54

    You can resolve the issue by forcing the collation used in a query to be a particular collation, e.g. SQL_Latin1_General_CP1_CI_AS or DATABASE_DEFAULT. For example:

    SELECT MyColumn
    FROM FirstTable a
    INNER JOIN SecondTable b
    ON a.MyID COLLATE SQL_Latin1_General_CP1_CI_AS = 
    b.YourID COLLATE SQL_Latin1_General_CP1_CI_AS
    

    In the above query, a.MyID and b.YourID would be columns with a text-based data type. Using COLLATE will force the query to ignore the default collation on the database and instead use the provided collation, in this case SQL_Latin1_General_CP1_CI_AS.

    Basically what's going on here is that each database has its own collation which "provides sorting rules, case, and accent sensitivity properties for your data" (from http://technet.microsoft.com/en-us/library/ms143726.aspx) and applies to columns with textual data types, e.g. VARCHAR, CHAR, NVARCHAR, etc. When two databases have differing collations, you cannot compare text columns with an operator like equals (=) without addressing the conflict between the two disparate collations.

提交回复
热议问题