create proc 提取记录
@read_user char(20) ---审核者
as
declare @id int
BEGIN TRANSACTION
--从本机,本人读取的记录中取
select top 1 @id=id from tablename with (UPDLOCK)
where 某字段=2
and [host_name]=host_name()
and read_user=@read_user
order by time
if @@rowcount=0 --如果没有,从没有被读取的记录中读取
begin
select top 1 @id=id from tablename with (UPDLOCK)
where 某字段=2
and(host_name is null
or read_user is null)
order by time
if @@rowcount=0 --如果没有,从超时的记录中读取
begin
select top 1 @id=id from tablename with (UPDLOCK)
where 某字段=2
and read_time<datediff(hout,-1,getdate()) --假设超时时间为1小时
order by time
if @@rowcount=0 --无记录退出
begin
commit tran
return
end
end
end
update tablename set
read_user=@read_user,
[host_name]=host_name(),
read_time=@read_time
where id=@id
commit transaction
select * from tablename where id=@id
go