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
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.
-
1@MartinSmith: many lolsRemus Rusanu– Remus Rusanu2014年12月12日 15:38:17 +00:00Commented Dec 12, 2014 at 15:38
-
4 seconds faster!Martin Smith– Martin Smith2014年12月12日 15:38:55 +00:00Commented Dec 12, 2014 at 15:38