SQLServer中防止并發(fā)插入重復(fù)數(shù)據(jù),大致有以下幾種方法:
1.使用Primary Key,Unique Key等在數(shù)據(jù)庫(kù)層面讓重復(fù)數(shù)據(jù)無(wú)法插入。
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 SERIALIZABLE Begin Tran select 1 from Table with(UPDLOCK) where **** --這里即算有索引支撐的情 況下,加的也是范圍鎖RangeS-U,雖然能鎖住,但并發(fā)性能也不佳。 if @@ROWCOUNT = 0 insert into Table (****) values(****); Commit Tran
提交成功!非常感謝您的反饋,我們會(huì)繼續(xù)努力做到更好!
這條文檔是否有幫助解決問(wèn)題?
還沒(méi)有賬號(hào)?立即 免費(fèi)注冊(cè)
賬號(hào)登錄還沒(méi)有賬號(hào)?立即注冊(cè)
服務(wù)熱線:4006-75-4006(7*24小時(shí)在線)
總機(jī)直撥:0871-63886388(工作日9:00-18:00)
售前咨詢
4006-75-4006
4006-75-4006(09:00-23:30) 0871-6388 6388 (總機(jī))(工作日 09:00-18:00)
售后咨詢
備案咨詢
0871-6388 6388
電話
二維碼
TOP