1

Why does this give me the same value, four 1?

USE [TSQL2012]
GO
IF OBJECT_ID('dbo.sqlsequence', 'SO') IS NOT NULL
 DROP SEQUENCE dbo.sqlsequence;
GO
CREATE SEQUENCE [dbo].[SQLSequence] AS INT 
START WITH 1
MAXVALUE 8 CYCLE
SELECT NEXT VALUE FOR
 [dbo].[SQLSequence] AS [Seq1] ,
 NEXT VALUE FOR
 [dbo].[SQLSequence] AS [Seq2] ,
 NEXT VALUE FOR
 [dbo].[SQLSequence] AS [Seq3] ,
 NEXT VALUE FOR
 [dbo].[SQLSequence] AS [Seq4]

But this works fine as expected. Gives me 1, 2, 3, 4

USE [TSQL2012]
GO
IF OBJECT_ID('dbo.sqlsequence', 'SO') IS NOT NULL
 DROP SEQUENCE dbo.sqlsequence;
GO
CREATE SEQUENCE [dbo].[SQLSequence] AS INT 
START WITH 1
MAXVALUE 8 CYCLE
SELECT NEXT VALUE FOR
 [dbo].[SQLSequence] AS [Seq1]
SELECT NEXT VALUE FOR
 [dbo].[SQLSequence] AS [Seq2]
SELECT NEXT VALUE FOR
 [dbo].[SQLSequence] AS [Seq3]
SELECT NEXT VALUE FOR
 [dbo].[SQLSequence] AS [Seq4]
asked Dec 12, 2014 at 15:29

1 Answer 1

4

As per the documentation states:

The NEXT VALUE FOR function is nondeterministic, and is only allowed in contexts where the number of generated sequence values is well defined. Below is the definition of how many values will be used for each referenced sequence object in a given statement:

SELECT - For each referenced sequence object, a new value is generated once per row in the result of the statement.

answered Dec 12, 2014 at 15:36
2
  • 1
    @MartinSmith: many lols Commented Dec 12, 2014 at 15:38
  • 4 seconds faster! Commented Dec 12, 2014 at 15:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.