sql server - How to UPDATE TOP(n) with ORDER BY giving a predictable result? -
i'm trying read top 100 items of database table being used queue. i'm trying mark items done this:
update top(@qty) queuetable (readpast) set isdone = 1 output inserted.id, inserted.etc queuetable isdone = 0 order createddate asc;
the problem is, according update (transact-sql) on msdn, order by
not valid in update
and:
the rows referenced in top expression used insert, update, or delete not arranged in order.
how can achieve need update items @ top of queue while selecting them?
sql server allows update derived table, cte or view:
update x set isdone = 1 output inserted.id, inserted.etc ( select top (n) * queuetable isdone = 0 order createddate asc; ) x
no need compute set of ids first. faster , has more desirable locking behavior.
Comments
Post a Comment