2011-08-12 114 views
0

我創建一個表中的一些過濾非羣集上的一些列,比如:過濾非聚集索引異常behaivior

CREATE UNIQUE NONCLUSTERED INDEX [IX_Sh_Esh] ON [dbo].[My_Tbl] 
(
    [City_Code] ASC, 
[Sh_Esh] ASC 
) 
WHERE ([Sh_Bod]=(0) AND [Noe_Fa]=(0)) 
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] 
    GO 

和:

CREATE UNIQUE NONCLUSTERED INDEX [IX_Kho] ON [dbo].[My_Tbl] 
(
[City_Code] ASC, 
[Kho] ASC 
) 
WHERE ([Sh_Bod]=(0) AND [Sh_Esh]=(0) AND [Noe_Fa]=(1)) 
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] 
GO 

我創造我的表這個指標與沒有錯誤,但當我想要添加一個新的列我得到這個錯誤:

'My_Tbl' table 
- Unable to create index 'IX_Sh_Esh'. 
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'My_Tbl' and the index name 'IX_Sh_Esh'. The duplicate key value is (3, 0). 

聲明已終止。

我的表中的數據是:

enter image description here

根據第一索引,因爲行1號和4號不滿足其中cluase不應該them.Why創建索引我得到上面的錯誤?

感謝

EDIT 1)

有有趣的點時,若我刪除索引,並添加列(軟件),然後重新創建索引,索引沒有error.STRANGE創造! !

+0

表中只有6行? – JNK

+0

沒有,還有更多的錯誤,但只顯示(3,0)爲複製 – Arian

+2

所以你做了一個'SELECT * FROM dbo.My_Tbl WHERE CITY_CODE = 3,Sh_Esh = 0',看是否有其他行? – JNK

回答

1

你可以刪除索引,然後添加數據,然後重新創建索引的原因是因爲創建索引不檢查現有數據,您試圖插入/更新僅僅是數據。

,因爲其篩選的索引,你得到它,因爲它的唯一索引,並且你想重複值添加到表中的列的唯一索引是你沒有得到錯誤。這是他們的優點!如果您需要重複數據,請勿使索引唯一。

+0

謝謝,但請閱讀我的問題fully.I'm不是要添加數據我想與當前正確的數據添加新列 – Arian

+0

'創造出獨特的index'不檢查現有數據創建索引時。 –