关于防止并发插入重复数据

SQLServer中防止并发插入重复数据,大致有以下几种方法:

1.使用Primary Key,Unique Key等在数据库层面让重复数据无法插入。

2.插入时使用条件

insert into Table(****) select **** where not exists(select 1 from Table where ****);

3.使用SERIALIZABLE隔离级别,并且使用updlock或者xlock锁提示(等效于在默认隔离级别下使用(updlock,holdlock)或(xlock,holdlock))

set transaction isolation level SERIALIZABLE
Begin
Tran select 1 from Table with(UPDLOCK) where **** --这里即算有索引支撑的情况下,加的也是范围锁RangeS-U,虽然能锁住,但并发性能也不佳。 if @@ROWCOUNT = 0 insert into Table (****) values(****); Commit Tran

本文链接:http://www.cnblogs.com/ajiangg/p/6520433.html

posted on 2017-03-10 16:46  万剑齐发  阅读(2016)  评论(3编辑  收藏  举报