我想也许你正在做类似的事情:
declare @id int, @phone varchar(20), @name varchar(100)
select top (1) @id = id, @phone = phone, @name = name
from People
where status = 'awaiting'
update People
set status = 'in_process'
where id = @id --previously grabbed
--- etc.
这是不安全的,因为其他一些进程也可能在您的更新语句之前选择相同的记录
在这种情况下,您可以将update
语句(或delete
,取决于您的逻辑)与输出子句一起使用
declare @person table (id int, phone varchar(20), name varchar(100))
update top (1) p
set status = 'in_process'
output inserted.id, inserted.phone, inserted.name into @person
from People p
where status = 'awaiting'