Hi all
question regarding how transactions work in SQL Server...
stored procedure below that generates unique IDs for messages we
send to the backend It looks like sometimes, the number generated is
identical for a messages that were sent simultaneously. Since this
seems like a concurrency issue, am I correct in thinking that using
SQL
transactions within the stored procedure code will solve this problem?
The stored procedure is outlined below, the code in red is what I
think should go in to solve the concurrency problem.
CREATE PROCEDURE [dbo].[praCCGetNewCDMIndex]
@newdate int
AS
SET NOCOUNT ON
DECLARE @lastdate int
DECLARE @lastindex int
DECLARE @newindex int
BEGIN TRAN CDMIndex
SELECT @lastdate = N_JULIANDATE FROM tbl_CC_CDMIndexes
SELECT @lastindex = N_INDEX FROM tbl_CC_CDMIndexes
IF @lastdate = @newdate
SET @newindex = @lastindex + 1
ELSE
SET @newindex = 1
UPDATE tbl_CC_CDMIndexes
SET N_JULIANDATE = @newdate,
N_INDEX = @newindex
SELECT N_INDEX FROM tbl_CC_CDMIndexes
COMMIT TRAN CDMIndex
SET NOCOUNT OFF
GO