I'm trying to read the top 100 items of a database table that is being used like a queue. As I do this I'm trying to mark the items as done like this:
UPDATE TOP(@qty)
QueueTable WITH (READPAST)
SET
IsDone = 1
OUTPUT
inserted.Id,
inserted.Etc
FROM
QueueTable
WHERE
IsDone = 0
ORDER BY
CreatedDate ASC;
The only problem is, according to UPDATE (Transact-SQL) on MSDN, the ORDER BY
is not valid in an UPDATE
and:
The rows referenced in the TOP expression used with INSERT, UPDATE, or DELETE are not arranged in any order.
How can I achieve what I need which is to update the items at the top of the queue while also selecting them?