In the GL journal voucher table I set identity "JV_ID" as bigint datatype with identity (so I can avoid getting max (JV_ID) then adding +1) , but in my database journal voucher table ID's should start with year ID as following (202100001,202100002,202200001,202200002), then I found threat talking about SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
in SQL server, So my questions:
Is setting isolation Level will really avoid duplication in getting same max ID in stored procedures?
How could I write the SP for the following table data:
JV_ID NVARCHAR 25 JV_YEAR NVARCHAR 4 JV_DATE DateTime JV_TOT Money JV_USER INT
I found this code in this thread link How to get Maximum ID value before creating a new record?
CREATE PROCEDURE usp_CreateNewIssue
@catagoryId int,
@issueId int OUTPUT,
@issueNumber int OUTPUT
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
declare @maxId int
declare @newRow table (IssueID int NOT NULL, IssueNumber int NOT NULL)
select @maxId = MAX(IssueNumber) from Issue where CategoryID = @catagoryId
Insert Into Issue (CategoryID, IssueNumber) values (@catagoryId, @maxId + 1)
OUTPUT INSERTED.IssueID, INSERTED.IssueNumber
INTO @newRow
select @issueId = IssueID, @issueNumber = IssueNumber from @newRow
COMMIT TRANSACTION
END