0
我已經創建了我的桌子複合唯一索引:SQL Server 2008:在唯一索引中忽略排序規則?
CREATE TABLE [dbo].[SearchIndexWord](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CatalogID] [int] NOT NULL,
[Word] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_SearchIndexWord] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
ON [PRIMARY]
CREATE UNIQUE NONCLUSTERED INDEX [IX_SearchIndexWord] ON [dbo].[SearchIndexWord]
(
[Word] ASC,
[CatalogID] ASC
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
整個數據庫排序規則設置爲SQL_Latin1_General_CP1_CI_AS。當我運行下面的T-SQL,它打印「不等於」:
IF 'm3/h' = 'm³/h'
PRINT 'Equals'
ELSE
PRINT 'Does not equal'
然後,如果我嘗試以下插入語句:
INSERT INTO [SearchIndexWord] (Word, CatalogID) VALUES ('m3/h', 1), ('m³/h', 1)
我收到以下錯誤信息:
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.SearchIndexWord' with unique index 'IX_SearchIndexWord'.
這是爲什麼?我無法在文檔中找到它,但我認爲使用配置的排序規則檢查了兩個鍵被複制的情況。
我已經檢查了表格,列和索引排序方式,它們都等於數據庫排序規則。
你說得對。我通過給列中的不同排序規則(SQL_Latin1_General_BIN)解決了問題。此外,它的工作方式如下:'IF N'm3/h'=N'm³/ h'' – ErikHeemskerk 2010-08-18 06:09:30
CAST()方法的工作原理是將兩個字符串轉換爲unicode。一個更快的/更簡單的方法做到這一點是像這樣: IF N'm3/H「=N'm³/ H」 PRINT‘等於’ ELSE PRINT‘不等於’ 通過將所述N個標記在前面每個字符串,你告訴SQL Server將每個字符串視爲unicode - 然後我得到'Equals'。 同樣,您也可以使用COLLATE子句: http://www.sqlmag.com/blog/practical-sql-server-45/tsql/Collation-SQL-Server-139576 – 2011-06-23 03:15:39