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

MSSQL
SQL Server中容易混淆的数据类型
影响SQL Server性能的主要原因
浅谈SQL Server查询优化器中的JOIN算法
怎样缩小SQL Server数据库日志文件
SQL Server 2005最值得你升级的10个理由
SQL Server 商务智能特性对比
SQL Server 2005 SP1的新特性
怎样使用SQL Server来过滤数据
在SQL Server中处理空值时涉及的三个问题
怎样才能保护好 SQL Server 数据库
SQL SERVER数据库口令的脆弱性
SQL Server中的动态和静态内存分配
SQL存储过程学习:特殊的存储过程-触发器
几条常见的数据库分页 SQL 语句
SQL Server 2008服务器合并功能介绍
SQL Server数据库开发的二十一条军规
SQL Server 2008 的管理能力
快速删除重复记录,SQL Server如何实现?
将MySQL数据导入到Sql Server中
SQL Server和MySQL的安全性分析

MSSQL 中的 SQLServer2005 的查询独占模拟


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 94 ::
收藏到网摘: 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
*/

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