当前位置: 首页 > 图文教程 > 数据库 > MSSQL > SQLServer2005 的查询独占模拟

MSSQL
SQL2005学习笔记 APPLY 运算符
SQL2005学习笔记 EXCEPT和INTERSECT运算符
SQL2005 学习笔记 窗口函数(OVER)
SQL2005 ROW_NUMER实现分页的两种常用方式
SQL Server中的XML数据进行insert、update、delete
在安装sql2005中或安装后sa用户无法登陆系统解决方法
SQL2005 自动备份的脚本
在SQLServer 2005中编写存储过程
SQL2005 服务器因重装改名后出错的说明
使用c#构造date数据类型
SQLServer2005 批量查询自定义对象脚本
SQLServer2005 Output子句获取刚插入的ID值
SQLServer 设置单词首字母大写
SQLServer ntile获取每组前10%的数据
安装SQL2005提示 找不到任何SQL2005组件的问题解决方案
sql2005开发版 没有任何功能可以安装
超详细的sql2005图解安装全过程
sqlserver2005 安装图解教程
winXP系统安装SQLServer2005开发版具体过程与注意问题
SQLServer 2005系统配置要求官方说明

MSSQL 中的 SQLServer2005 的查询独占模拟


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 89 ::
收藏到网摘: n/a

这个问题一直被很多人关注,基本上得到的答案是两种倾向,一种是锁一种是给记录打标记(也就是update)。 对于应用来说,我并不提倡人为给记录加锁,这样会惹来很多麻烦,况且锁并不能解决所有问题,如果你有这方面好的经验我们可以进一步交流。
对于应用来说,我并不提倡人为给记录加锁,这样会惹来很多麻烦,况且锁并不能解决所有问题,如果你有这方面好的经验我们可以进一步交流。
复制代码 代码如下:

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
*/

如果你有更好的建议,我们不妨探讨一下。