Several web servers access SQL Server to obtain numerical code, when this code does not exist, it must be autogenerated by SQL Server.
I need to make sure that even if two simultaneous calls arrive and the code does not exist, only one code is generated, and both calls return the same code. So I have to do something like this:
begin lock
if code exists
return code
else
generate code
return code
end lock
I read a little about isolation levels and table locking, but I have a terrible mess with all of this. At first I thought the SERIALIZABLE isolation level is what I need, but apparently it is not.
So, what would you do to perform a “lock” in TSQL?
Many thanks.
UPDATE:
, , this :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE get_code
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
select code from codes where granted is null;
END
GO
Msg 1018, 15, 1, get_code, 4 'SERIALIZABLE. , A WITH . SQL Server . Msg 102, 15, 1, 5 "END".
?