I have a table with the following structure:
ID NAME STATUS
1 Item1 Pending
2 Item2 Pending
3 Item3 Pending
........................
I use a stored procedure to update the first N rows of the table, with status "Pending", to status 'Processing' and return the affected rows. Snippet:
UPDATE TOP (@size) sample_table
SET status = "PROCESSING"
OUTPUT inserted.*
WHERE status = 'PENDING'
This works as intended. Now an issue arises when I have multiple instances of my application running the SQL code script above.
I need to ensure that no 2+ instances run the above on the same data.
I can simply lock the table for the operation but I will have thousands of entries in that table in the future so this will hamper performance.
I have been reading up on ROWLOCK and it seems to do what I require but I have seen accounts where ROWLOCK was ignored(or elevated to table lock) by the query optimizer.
Is using ROWLOCK appropriate for my use case? Is there a better approach to this problem?