Prevent a Stored Procedure from being executed twice at the same time

后端 未结 5 817
甜味超标
甜味超标 2021-01-02 17:10

I have a stored procedure for SQL Server 2000 that can only have a single instance being executed at any given moment. Is there any way to check and ensure that the procedur

5条回答
  •  青春惊慌失措
    2021-01-02 17:28

    At the start of the procedure check if piece of data is 'locked' if not lock it

    At end of procedure unlock the piece of data.

    ie

    SELECT @IsLocked=IsLocked FROM CheckLockedTable Where spName = 'this_storedProcedure'
    
    IF @IsLocked = 1
        RETURN
    ELSE
        UPDATE CheckLockedTable SET IsLocked = 1 Where spName = 'this_storedProcedure'
    
    .
    .
    .
    
    -- At end of Stored Procedure
        UPDATE CheckLockedTable SET IsLocked = 0 Where spName = 'this_storedProcedure'
    

提交回复
热议问题