En løsning på dette problem fra "Inside Microsoft SQL Server 2008:T-SQL Querying"
CREATE TABLE dbo.Sequence(
val int IDENTITY (10000, 1) /*Seed this at whatever your current max value is*/
)
GO
CREATE PROC dbo.GetSequence
@val AS int OUTPUT
AS
BEGIN TRAN
SAVE TRAN S1
INSERT INTO dbo.Sequence DEFAULT VALUES
SET @val=SCOPE_IDENTITY()
ROLLBACK TRAN S1 /*Rolls back just as far as the save point to prevent the
sequence table filling up. The id allocated won't be reused*/
COMMIT TRAN
Eller et andet alternativ fra den samme bog, der tildeler intervaller lettere. (Du skal overveje, om du skal kalde dette indefra eller uden for din transaktion - indefra vil blokere andre samtidige transaktioner, indtil den første forpligter sig)
CREATE TABLE dbo.Sequence2(
val int
)
GO
INSERT INTO dbo.Sequence2 VALUES(10000);
GO
CREATE PROC dbo.GetSequence2
@val AS int OUTPUT,
@n as int =1
AS
UPDATE dbo.Sequence2
SET @val = val = val + @n;
SET @val = @val - @n + 1;