SQL Server 2005 和 SELECT 和 UPDATE 锁定
我想执行更新然后选择结果。在选择发生之前,我不希望任何内容能够更新我正在更新的行。我该怎么做?
我的目标是增加一行的值并返回该增加的值。到目前为止,我发现我最终遇到了一个问题,在两个查询几乎同时发生的情况下,更新(增加)后跟一个选择,选择似乎返回相同的数字。所以我猜测像 update > 这样的东西更新>选择>选择正在发生。
我错过了将其标记为 SQL Server 2005。我实际上正在使用 Server 2000。因此输出子句不起作用(不在该版本中)。
BEGIN TRANSACTION
UPDATE Table SET Last=(Last+1) WHERE ID=someid;
SELECT * FROM Table WHERE ID=someid;
COMMIT TRANSACTION
I want to perform a update then select the result. I don't want anything to be able to update the row I am updating until after the select has occurred. How would I do this?
My goal is to increment a value of a row and return that incremented value. I have thus far found that I end up with an issue where update (to increment) followed by a select in a situation where two queries happen at near the same time the selects seem to return the same number. So I am guessing that something like update > update > select > select is happening.
I miss labeled this as SQL Server 2005. I am actually working with Server 2000. So the output clause does not work (is not in that version).
BEGIN TRANSACTION
UPDATE Table SET Last=(Last+1) WHERE ID=someid;
SELECT * FROM Table WHERE ID=someid;
COMMIT TRANSACTION
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使在已提交读的默认事务隔离级别下也应该执行此操作。
您还可以使用 OUTPUT 子句直接返回行更新。这个例子
Should do it even at the default transaction isolation level of read committed.
You could also use the OUTPUT clause to get the row directly back after the update. Example of this