I have 2 SQL statements, that can be executed concurrently.
1st one updates request from Requests table, when request is taken for processing:
UPDATE Requests
SET IsInProcess = '1'
2nd one counts requests in process:
SELECT COUNT(*)
FROM Requests
WHERE IsInProcess = '1'
I need to count requests only after update statement is finished.
What transaction isolation levels or table hints do I need to use to accomplish this ?
You don’t have to use any.
READ COMMITTEDand default locking will do that. (Provided the update starts a bit earlier, otherwise the update will wait until you count.)Given your
whereclause, it’s likely the server will takeXlock on the whole table. Event if it doesn’t, the losing process will wait until the winning process releases its lock on the first updated/read row.