SQLServer中防止并發(fā)插入重復(fù)數(shù)據(jù),大致有以下幾種方法:
1.使用Primary Key,Unique Key等在數(shù)據(jù)庫層面讓重復(fù)數(shù)據(jù)無法插入。
2.插入時(shí)使用條件
insert into Table(****) select **** where not exists(select 1 from Table where ****);
3.使用SERIALIZABLE隔離級(jí)別,并且使用updlock或者xlock鎖提示(等效于在默認(rèn)隔離級(jí)別下使用(updlock,holdlock)或(xlock,holdlock))
set transaction isolation level SERIALIZABLEBegin Tran select 1 from Table with(UPDLOCK) where **** --這里即算有索引支撐的情況下,加的也是范圍鎖RangeS-U,雖然能鎖住,但并發(fā)性能也不佳。 if @@ROWCOUNT = 0 insert into Table (****) values(****);Commit Tran
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持VeVb武林網(wǎng)!
新聞熱點(diǎn)
疑難解答
圖片精選