對於應用來說,我並不提倡人為給記錄加鎖,這樣會惹來很多麻煩,況且鎖並不能解決所有問題,如果你有這方面好的經驗我們可以進一步交流。
對於應用來說,我並不提倡人為給記錄加鎖,這樣會惹來很多麻煩,況且鎖並不能解決所有問題,如果你有這方面好的經驗我們可以進一步交流。
復制代碼 代碼如下:
set nocount on
use tempdb
go
if (object_id ('tb' ) is not null )
drop table tb
go
create table tb (id int identity (1 , 1 ), name varchar (10 ), tag int default 0 )
insert into tb (name ) select 'a'
insert into tb (name ) select 'b'
insert into tb (name ) select 'c'
insert into tb (name ) select 'd'
insert into tb (name ) select 'e'
go
update top (2 ) tb with (rowlock , readpast ) set tag = 1 output inserted . id , inserted . name where tag = 0
go
update top (2 ) tb with (rowlock , readpast ) set tag = 1 output inserted . id , inserted . name where tag = 0
go
update top (2 ) tb with (rowlock , readpast ) set tag = 1 output inserted . id , inserted . name where tag = 0
go
set nocount off
/*
id name
----------- ----------
1 a
2 b
id name
----------- ----------
3 c
4 d
id name
----------- ----------
5 e
*/
如果你有更好的建議,我們不妨探討一下。