create table MyTable2 (
[a] decimal(18,2) not null,
[b] decimal(18,2) not null,
[c] decimal(18,2) not null,
[d] decimal(18,2),
CONSTRAINT myPK PRIMARY KEY (a,b,c)
)
CREATE UNIQUE INDEX MyUniqueIgnoringDups
ON MyTable2 (a,b,c)
WITH IGNORE_DUP_KEY --SQL 2000 syntax
--WITH(IGNORE_DUP_KEY = On) --SQL 2005+ syntax
--insert some data to test.
insert into mytable2 (a,b,c,d) values (1,2,3,4);--succeeds; inserts properly
insert into mytable2 (a,b,c,d) values (1,2,3,5);--insert fails, no err is raised.
-- "Duplicate key was ignored. (0 row(s) affected)"
任何有興趣,這裏是什麼從Erland Sommarskog on the MSDN forums發生的解釋:
When IGNORE_DUP_KEY
is OFF, a duplicate key value causes an error and the entire statement is rolled back. That is, if the statement attempted to insert multiple rows, no rows are inserted.
When IGNORE_DUP_KEY
is ON, a duplicate key value is simply ignored. The statement completes successfully and any other rows are inserted.
什麼是IGNORE_DUP_KEY主鍵的含義?它看起來像一個設計缺陷! – jachguate 2011-04-05 01:11:37
我期待這樣的評論:),但只要你考慮到所有方面(好與壞),我不認爲這是一個設計缺陷 - >這是現在對我的工作。我希望我的項目是唯一的,但我不想在插入過程中驗證它們的唯一性。我已經看到它可以使用連接手動完成,但我懷疑它和使用IGNORE_DUP_KEY一樣快。如果你願意,我可以更多地描述我將在哪裏使用它。 – 2011-04-05 01:18:47