1

我有存储过程:

ALTER PROCEDURE [dbo].[myDbProcedure]
@ID int

AS
BEGIN 
SET NOCOUNT ON;

IF EXISTS (SELECT * FROM [myDB] WHERE @ID = [ID])
UPDATE [myDB]
SET [count] = [count] - 1
, [paymentCount]  = [paymentCount]  + 1
WHERE @ID = [ID] 

END

现在,如果 100 个人同时更新表格任何信息都不会错过,我想要安全

4

1 回答 1

2

不知道我是否完全理解您的要求:

Begin Tran
    IF EXISTS (SELECT * FROM [myDB] WHERE @ID = [ID])
    UPDATE [myDB]
    SET [count] = [count] - 1
    , [paymentCount]  = [paymentCount]  + 1
    WHERE @ID = [ID] 
Commit

您还可以考虑删除 if,一旦该更新仅在寄存器实际存在时才会发生。这样,您将获得本质上在事务上下文中运行的更新。考虑查看隔离级别(来自评论:设置事务隔离级别可序列化)。

于 2013-05-30T11:10:18.520 回答