給出一個觀點列忽略創建唯一索引
CREATE VIEW MyView WITH SCHEMABINDING AS
SELECT A, B, Year, t1.FKId, C, D, COUNT_BIG(*) as Cnt, SUM(W) As Wgt
FROM [dbo].[T1] t1
INNER JOIN [dbo].T2 t2 ON t1.FKId = t2.Id
INNER JOIN [dbo].T3 t3 ON t1.FKId = t3.FKId AND t1.FKId = t3.FKId
GROUP BY A, B, Year, t1.FKId, C, D
當我試圖使該索引視圖
CREATE UNIQUE CLUSTERED INDEX IX_CLU_My
ON MyView (FKId, A, B, Year, C, D)
的SQL Server抱怨重複鍵,但錯誤消息只考慮5指定了6列:
CREATE UNIQUE INDEX語句終止,因爲找到了對象名稱'dbo的重複鍵.MyView'和索引名稱'IX_CLU_My'。重複鍵值是(934,1,6,2014,1)。
存在於對應於第一鍵4的視圖中的實際數據是:
FKId A B Year C D
934 1 6 2014 1 5
934 1 6 2014 1 15
934 1 6 2014 2 5
934 1 6 2014 3 5
考慮到第一5列,該錯誤消息將是正確的。爲什麼第六列不考慮唯一性?
您只有4行從您的視圖返回? –
@RickS:不,這些是與這場衝突有關的。視圖中總共有幾十萬行。 –