I have stored procedure in the sql server 2008, my stored procedure calculate and get the last number "not primary key" from column from table B and add one ( +1 ) to this number to use it on the next statement on the same stored procedure.
My issue that i have a duplicate number some times, i think this happened when multiple users call the stored procedure on the same time. is this the issue and how can i solve it
my code is like the below:-
DECLARE @ID AS NVARCHAR(10)
SET @ID = (
SELECT TOP 1 MyNo
FROM Employee
WHERE (
(TypeID = @TypeID) AND
(Year = @Year)
)
ORDER BY ID DESC
)
SET @ID = ISNULL(@ID,0) + 1
INSERT INTO Employee (name,lname,MyNo) VALUES (@name,@lname,@MyNo)