34,587
社区成员
发帖
与我相关
我的任务
分享
begin tran
declare @N int
select @N=[num] from [T] where [id]=1
if(@N<2)
update [T] set [num]=[num]+1 where [id]=1
commit tran
select @N=[num] from [T] where [id]=1
select @N=[num] from [T] with(xlock,paglock) where [id]=1
第一个用户开始:
begin tran
declare @N int
select @N=[num] from [T] where [id]=1
waitfor delay '00:00:10'
if(@N<2)
update [T] set [num]=[num]+1 where [id]=1
waitfor delay '00:00:05'
commit tran
--然后,随着第二个用户开始:
begin tran
declare @N int
select @N=[num] from [T] where [id]=1
waitfor delay '00:00:20'
if(@N<2)
update [T] set [num]=[num]+1 where [id]=1
commit tran
--试试这样,看会不会发生你说的情况....
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
begin tran
declare @N int
select @N=[num] from [T] with (rowlock) where [id]=1
if(@N<2)
update [T] set [num]=[num]+1 where [id]=1
commit tran
1 如何锁一个表的某一行
A 连接中执行
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
begin tran
select * from tablename with (rowlock) where id=3
waitfor delay '00:00:05'
commit tran
B连接中如果执行
update tablename set colname='10' where id=3 --则要等待5秒
update tablename set colname='10' where id<>3 --可立即执行
begin tran
declare @N int
select @N=[num] from [T] where [id]=1--两用户同一时间点执行存储过程,取得@N的值都为1
if(@N<2)--都符合此条件
update [T] set [num]=[num]+1 where [id]=1--两用户都执行了这句,num的值变为了3
commit tran
--是这个意思吗?没发现问题啊...
create table t(id int, num int)
insert into t
select 1,1
begin tran
declare @N int
select @N=[num] from [T] where [id]=1
if(@N<2)
update [T] set [num]=[num]+1 where [id]=1
commit tran
begin tran
declare @n1 int
select @n1=[num] from [T] where [id]=1
if(@n1<2)
update [T] set [num]=[num]+1 where [id]=1
commit tran