SQLServer2005 的查詢獨占模擬
更新時間:2009年09月06日 01:13:16 作者:
這個問題一直被很多人關注,基本上得到的答案是兩種傾向,一種是鎖一種是給記錄打標記(也就是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
*/
如果你有更好的建議,我們不妨探討一下。
對于應用來說,我并不提倡人為給記錄加鎖,這樣會惹來很多麻煩,況且鎖并不能解決所有問題,如果你有這方面好的經驗我們可以進一步交流。
復制代碼 代碼如下:
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
*/
如果你有更好的建議,我們不妨探討一下。
相關文章
SQLServer2005與SQLServer2008數(shù)據(jù)庫同步圖文教程
要實現(xiàn)SQLServer2005與2005的數(shù)據(jù)庫同步的話,直接用鏡像就可以實現(xiàn)。但是如果同步 SQLServer2008的話,2005的實例是連接不上08的。低版本的無法連接高版本的。所以我們可以通過復制的方式,也就是所謂的訂閱發(fā)布的方法來實現(xiàn)兩個不同版本數(shù)據(jù)庫的數(shù)據(jù)同步。2011-09-09簡述SQL Server 2005數(shù)據(jù)庫鏡像相關知識
本文我們主要介紹了SQL Server 2005數(shù)據(jù)庫鏡像的相關知識,包括數(shù)據(jù)庫鏡像的優(yōu)點、數(shù)據(jù)庫鏡像的工作方式等,需要的朋友可以參考下2015-08-08sqlserver中獲取當前日期的午夜的時間值的實現(xiàn)方法
MS SQLServer,如何獲取當前系統(tǒng)日期的午夜的時間值的方法,需要的朋友可以參考下。2011-09-09SQLServer2005混合模式登錄配置(用戶登錄錯誤18452,233,4064)
這篇文章主要介紹了SQLServer2005混合模式登錄配置(用戶登錄錯誤18452,233,4064),需要的朋友可以參考下2014-07-07