I think your one stored procedure can do lock and unlock (used with "Select @strNewMax As NewMax") ...
Here is an example from the system:
Declare @strNewMax Char Select @strNewMax = 'N' BEGIN TRANSACTION If @BidAmount > (Select Max(AB_Bid_AMT) from AuctionBid With(updlock, holdlock) Where AB_AI_ID = @AuctionItemId) Begin Insert Into AuctionBid (AB_AI_ID, AB_Bid_AMT, AB_Emp_ID, AB_Entry_DTM) Select @AuctionItemId, @BidAmount, @EmployeeId, GetDate() Select @strNewMax = 'Y' End COMMIT TRANSACTION Select @strNewMax As NewMax
This will insert the record as the next highest bid, with the entire blocking of the entire table, so that other bets will not be processed at the same time. It will return either "Y" or "N" depending on whether it works or not.
Perhaps you can accept this and customize it according to your application.
Mikey source share