Is PK the primary key? Then this is not a problem, if you already know the primary key, there is no sport. If pk is the primary key, then the obvious question arises, how do you know that the pk of an element is deactivated ...
The problem is that you do not know the primary key and want to delete the next βavailableβ (that is, status = y) and mark it as dequeued (delete it or set status = z).
The right way to do this is to use a single statement. Unfortunately, the syntax is different between Oracle and SQL Server. SQL Server Syntax:
update top (1) [<table>] set status = z output DELETED.* where status = y;
I'm not good enough at the Oracle RETURNING clause to give an example similar to SQL OUTPUT one.
Other SQL Server solutions require proper SELECT lock indications (with UPDLOCK). In Oracle, the preferred aspect is using FOR UPDATE, but this does not work in SQL Server, since FOR UPDATE must be used with cursors in SQL.
In any case, the behavior that you have in the original message is incorrect. Several sessions can select the same row (s), and even update all of them, returning the same elements (s) for several readers.
Remus Rusanu
source share