Skip to content

Instantly share code, notes, and snippets.

@lukencode
Created February 25, 2013 04:34
Show Gist options
  • Save lukencode/5027766 to your computer and use it in GitHub Desktop.
Save lukencode/5027766 to your computer and use it in GitHub Desktop.
CREATE PROCEDURE spNextStudySequence
@StudyID int
AS
DECLARE @temp table
(
ID bigint
);
UPDATE tStudy_Sequences
SET Sequence = Sequence + 1
OUTPUT INSERTED.Sequence into @temp
WHERE StudyID = @StudyID;
IF NOT EXISTS (SELECT 1 FROM @temp)
BEGIN
INSERT INTO tStudy_Sequences(StudyID, Sequence) VALUES(@StudyID, 1)
INSERT INTO @temp VALUES(1)
END
SELECT TOP(1) * FROM @temp
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment