I have two concurrent processes and I have two queries, eg.:
select top 10 * into #tmp_member
from member
where status = 0
order by member_id
and then
update member
set process_status = 1
from member inner join #tmp_member m
on member.member_id=m.member_id
I’d like each process to select different rows, so if a row was already selected by the first process, then do not use that one in the second process’ result list.
Do I have to play around with locks? UPDLOCK, ROWLOCK, READPAST hints maybe? Or is there a more straightforward solution?
Any help is appreciated,
cheers,
b
You need hints.
See my answer here: SQL Server Process Queue Race Condition
However, you can shorten your query above into a single statement with the OUTPUT clause. Otherwise you’ll need a transaction too (asuming each process executes the 2 statements above one after the other)
Summary: if you want multiple processes to
select 10 rows where status = 0set process_status = 1…then use this code.