gpt4 book ai didi

SQL Server 乐观锁定 - 返回更改的时间戳值

转载 作者:行者123 更新时间:2023-12-01 09:37:24 25 4
gpt4 key购买 nike

我有一个实现乐观锁定的更新存储过程。存储过程如下所示:

ALTER PROCEDURE [dbo].[usp_Test] 
@Id AS char(2),
@recordTimestamp as timestamp
...
BEGIN
UPDATE XY
..
WHERE ((Id = @Id) AND (recordTimeStamp = @recordTimestamp))

if @@rowcount = 0
begin
RAISERROR ('this row was changed by another user', 18, 1)
end

SELECT timeStamp from XY where Id = @Idend

有没有更简单的方法来返回新的时间戳?我真的很想避免 SELECT 语句。

最佳答案

假设至少 SQL Server 2005 可以使用 OUTPUT

UPDATE XY
SET Col = 'foo'
OUTPUT inserted.recordTimeStamp
WHERE ((Id = @Id) AND (recordTimeStamp = @recordTimestamp))

或者使用表变量更接近地反射(reflect)原始查询行为的版本。

DECLARE @Timestamp TABLE(stamp binary(8))

UPDATE XY
SET col='foo'
OUTPUT inserted.recordTimeStamp INTO @Timestamp
WHERE (Id = @Id) AND (recordTimeStamp = @recordTimestamp)

if @@rowcount = 0
begin
RAISERROR ('this row was changed by another user', 18, 1)
end

SELECT stamp
FROM @Timestamp

关于SQL Server 乐观锁定 - 返回更改的时间戳值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5312847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com