Skip to content

Instantly share code, notes, and snippets.

@kbaesler
Last active December 29, 2015 02:39
Show Gist options
  • Save kbaesler/7601830 to your computer and use it in GitHub Desktop.
Save kbaesler/7601830 to your computer and use it in GitHub Desktop.
There is no concept of sequences in SQL Server 2008 R2, thus a combination of tables and stored procedures will be used to create the desired behavior.
-- Create the sequencing table that is used to hold the sequence values.
CREATE TABLE SQL_SERVER_SEQUENCE (
NAME VARCHAR(30) NOT NULL,
VALUE BIGINT DEFAULT 0 NOT NULL,
CONSTRAINT PK_SQL_SERVER_SEQUENCE PRIMARY KEY (NAME)
);
-- Create the stored procedure that is responsible for incrementing the sequence and return it.
ALTER PROCEDURE SQL_SERVER_SEQUENCE_NEXTVAL
@name VARCHAR(30)
AS
BEGIN
DECLARE @value BIGINT
BEGIN TRANSACTION
UPDATE SQL_SERVER_SEQUENCE
SET @value = value = value + 1
WHERE name = @name;
COMMIT TRANSACTION
SELECT @value AS NEXTVAL
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment