Friday, March 27, 2009

Users get blocked when access same table while getting Unique IDs

--Table which will have the Unique IDs
CREATE TABLE dbo.CounterTable
(
Counter int IDENTITY(1,1) NOT NULL
)
GO
--Procedure which Inserts unique IDs in table and returns the New ID
CREATE PROCEDURE dbo.GetNextCounter
AS
BEGIN TRAN
DECLARE @Counter BIGINT

INSERT INTO dbo.CounterTable DEFAULT VALUES
SET @Counter = SCOPE_IDENTITY()
COMMIT
RETURN @Counter

GO

--Now Use the @Counter to give each user the Unique ID

DECLARE @Counter int
EXEC @Counter = dbo.GetNextCounter
SELECT @Counter

No comments: