要回答你的問題,首先要確定我們在同一頁面上。我們將與情侶表開始:表1上
CREATE TABLE dbo.table1(someText varchar(20)); -- uses default collation: SQL_Latin1_General_CP1_CI_AS
CREATE TABLE dbo.table2(someText varchar(20) collate Latin1_General_BIN);
someTxt使用我的服務器默認排序規則(SQL_Latin1_General_CP1_AS)。 table2上的同一列使用Latin1_General_BIN。
此查詢與給我錯誤,你看到。
SELECT *
FROM dbo.table1 t1
JOIN dbo.table2 t2
ON t1.someText = t2.someText;
錯誤:
Msg 468, Level 16, State 9, Line 23 Cannot resolve the collation conflict between "Latin1_General_BIN" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
有使用COLLATE兩種常用的方法來解決此錯誤:
選項1 - 在您的查詢
選項2 - 修改表格定義 這裏我將使用COLLATE更改table1上的列以匹配table2上的列。
ALTER TABLE dbo.table1
ALTER column someText varchar(20) collate Latin1_General_BIN;
現在我不必在查詢中定義排序規則。
你的問題
I understand I can use COLLATE to make sure both compared values have the same collation, but I was wondering if this has any kind of permanent effect on those tables, as in a permanent collation change, given they are needed as they are for other applications.
如果使用選項1走那麼答案是NO ;以這種方式使用COLLATE不會以任何方式改變表格。如果使用選項2,則YES - 列上的排序規則將永久更改。影響可能很大,也可能根本沒有影響(我對您的應用程序不夠了解)。
沒有。它會更改排序規則,如果可以,則執行等於操作。它不會永久改變任何東西。 https://docs.microsoft.com/en-us/sql/t-sql/statements/collations – Leonidas199x
你能告訴我們代碼示例嗎? – whisk
@whisk我添加了一個例子,但我需要的查詢本身非常通用。 – user1676874