sql With(NoLock),With(ReadPast)

本文通过具体实例演示了SQL中更新语句如何对数据库加锁,并探讨了readpast与nolock提示符的作用及其对查询结果的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---------------
create table tmp1
( 
    id int primary key,
    name nvarchar(20)
)

-----------
insert into tmp1(id,name) values(1,'zhsan1');
insert into tmp1(id,name) values(2,'lisi1');

-------------------------------
select * from tmp1


 

更新(Update)语句对数据库加锁

-----------------------------
begin tran tran1
    update tmp1 set name='zhsan' where id=1
-------------------------------
select * from tmp1

查询一直在进行,此时表已经被锁住了

-------------------------------
update tmp1 set name='lisi' where id=2
-------------------------------
select * from tmp1 where id>1

-------------------------------
delete from tmp1 where id=2
-------------------------------
select * from tmp1 where id>1

查询,更新,删除除被锁定的那行数据都能成功,说明被锁住的只有id=1的那一行,对其他行的查询,更新及删除无影响

插入语句

-------------------------------
insert into tmp1(id,name) values(3,'lisi')
-------------------------------
select * from tmp1 where id>1

结果执行成功,说明插入不受影响


 先将数据库还原为

-----------------------------
begin tran tran1
    update tmp1 set name='zhsan' where id=1

with(readpast)与with(nolock)

-------------------------------
select * from tmp1 with(readpast)

-------------------------------
select * from tmp1 with(nolock)

with(readpast)会忽略被锁住的那一行,with(nolock)不会

转载于:https://www.cnblogs.com/zhyue93/p/sql_nolock_readpast.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值