Microsoft SQL Server
--
Questions
--
Followers
Top Experts
I'm working with SQL 2008 R2.
I need a stored that will look through records in a table, identify the next one that should should be processed, mark that record so that no other calls to the same sp can be given the same record. Β This procedure will be called by a multi-threaded application so it could be called at the same time by multiple threads so it needs to cater for that. Β
Here's what I came up with and thought it had worked but I've found that it seems possible for 2 calls to the sp at the same time to both be given the same record back....
DECLARE @threadId int
BEGIN TRANSACTION
-- Get the next available thread
SELECT TOP 1 @threadId = threadId FROM vwThreads
WHERE timeToProcess < GETDATE()
AND thread_status = 'waiting'
ORDER BY last_pulled DESC
-- Update thread to prevent others from taking
UPDATE tblThreads
SET server_name = @serverName, thread_status = 'pulled',last_pulled = GETDATE()
WHERE threadId = @threadId
COMMIT TRANSACTION
-- Now select the full row of data for our thread to send back to application
SELECT TOP 1 * FROM vwThreads
WHERE threadId = @threadId
Can someone please spot what I've done wrong, or suggest a better way to achieve this functionality?
Many thanks in advance
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I actually managed to find something that I think may help, but some confirmation would be cool.
From what I've read, I should add WITH (UPDLOCK) to my initial select so that no other thread can get the same row until I have finished the transaction, so this should prevent my issue of 2 (or more) threads being able to pull down the same row (threadId)?
Cheers
Thanks for that, that sounds like a good way to go, didn't realise I could do it all in one statement. Β I will try to implement this into test system early next week and report back on how successful it has been in curing my problem.
Cheers
Just trying to implement your suggestion and got a little stuck on how to ensure that the 1 record I get is the record with the oldest last_pulled date, was trying similar to the following but get an error on the order by
UPDATE TOP (1) tblThreads
SET
server_name = @serverName,
thread_status = 'pulled',
last_pulled = GETDATE(),
@threadId = threadId
WHERE
timeToProcess < GETDATE() AND thread_status = 'waiting'
ORDER BY last_pulled DESC
Thanks






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
The way we solved this problem was to put a clustered index on time column in our processing queue table, forcing SQL Server to sort the physical data in ascending order on that key. While I always learned not to rely on the order of results in a statement that didn't include an ORDER BY clause, in practice, this effectively returns the oldest row in the table. It's not an official solution, but since order wasn't really critical for us and was really just a suggestion (best-effort instead of true FIFO), this took care of us.
Officially, the answer is to use a sub-query, like this:
UPDATE TOP (1) tblThreads
SET
server_name = @serverName,
thread_status = 'pulled',
last_pulled = GETDATE(),
@threadId = threadId
FROM tblThreads t1
JOIN (select min(last_pulled) as last_pulled from tblThreads
where thread_status = 'waiting') t2
ON t1.last_pulled = t2.last_pulled
WHERE
timeToProcess < GETDATE() AND thread_status = 'waiting'
That way, your inner query finds the oldest "Last_Pulled" value in the table (where the row is "waiting", whatever that means in your application) and then your outer query only pulls a single row with that value, so you'll get the oldest row in teh table (or one of, if there are multiples with that same datetime).
I hope this answers your question!
Thanks

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Microsoft SQL Server
--
Questions
--
Followers
Top Experts
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.