Detecting dirty reads from a stored procedure

前端 未结 8 511
自闭症患者
自闭症患者 2021-02-04 02:27

I\'ve got 100 threads that are each calling the stored procedure as defined below.

How do I prevent dirty reads?

SET QUOTED_IDENTIFIER OFF
SET ANSI_NULLS         


        
8条回答
  •  隐瞒了意图╮
    2021-02-04 02:56

    You can avoid the problem by using the @variable = column = expression syntax as described in the Books Online. Also, since the statement executes in an single-statement automatic transaction, you can avoid explicit transaction.

    SET QUOTED_IDENTIFIER ON;
    SET ANSI_NULLS ON;
    GO
    
    CREATE PROCEDURE GetNextSerialIdentity
          @NextKey int output
        , @TableID int
    AS
    SET NOCOUNT ON;
    
    UPDATE dbo.CfgSerialNumber
    SET @NextKey = SerialNumber = SerialNumber + 1
    WHERE SerialNumberID = @TableID;
    
    IF @@ROWCOUNT = 0
    BEGIN
    RAISERROR ('No Table Record Exists in CfgCerealNumber for ID:%d   ', 
                      16,1, @TableID);
    END
    GO
    

提交回复
热议问题