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