Last active
December 29, 2015 02:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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