I have this stored procedure
CREATE PROCEDURE spGrabSerial
@serial nvarchar(16) output
AS
BEGIN
SET NOCOUNT ON;
set @serial = (SELECT top 1 serial from tblSerial)
update tblSerial set InUse = 1 where serial = @serial
END
How can I make sure that no other procedure grabs the same serial in between the select and the update?
Assuming SQL Server 2005+ you can use the
OUTPUTclause to do it in one atomic operation (see Using tables as Queues).Edit Just been reminded of a way of doing this that can assign directly to the
outputparameter without using theOUTPUTclause at all.