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]

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.

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.