You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/t-sql/functions/next-value-for-transact-sql.md
+17-19Lines changed: 17 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,8 +35,7 @@ ms.author: jrasnick
35
35
36
36
## Syntax
37
37
38
-
```
39
-
38
+
```syntaxsql
40
39
NEXT VALUE FOR [ database_name . ] [ schema_name . ] sequence_name
41
40
[ OVER (<over_order_by_clause>) ]
42
41
```
@@ -168,7 +167,7 @@ NEXT VALUE FOR [ database_name . ] [ schema_name . ] sequence_name
168
167
169
168
The following examples use a sequence named `CountBy1` in a schema named `Test`. Execute the following statement to create the `Test.CountBy1` sequence. Examples C and E use the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] database, so the `CountBy1` sequence is created in that database.
170
169
171
-
```
170
+
```sql
172
171
USE AdventureWorks2012 ;
173
172
GO
174
173
@@ -184,7 +183,7 @@ GO
184
183
### A. Using a sequence in a select statement
185
184
The following example creates a sequence named `CountBy1` that increases by one every time that it is used.
186
185
187
-
```
186
+
```sql
188
187
SELECT NEXT VALUE FOR Test.CountBy1AS FirstUse;
189
188
SELECT NEXT VALUE FOR Test.CountBy1AS SecondUse;
190
189
```
@@ -202,10 +201,10 @@ SecondUse
202
201
### B. Setting a variable to the next sequence value
203
202
The following example demonstrates three ways to set a variable to the next value of a sequence number.
204
203
205
-
```
206
-
DECLARE @myvar1 bigint = NEXT VALUE FOR Test.CountBy1
207
-
DECLARE @myvar2 bigint ;
208
-
DECLARE @myvar3 bigint ;
204
+
```sql
205
+
DECLARE @myvar1 BIGINT= NEXT VALUE FOR Test.CountBy1
206
+
DECLARE @myvar2 BIGINT ;
207
+
DECLARE @myvar3 BIGINT ;
209
208
SET @myvar2 = NEXT VALUE FOR Test.CountBy1 ;
210
209
SELECT @myvar3 = NEXT VALUE FOR Test.CountBy1 ;
211
210
SELECT @myvar1 AS myvar1, @myvar2 AS myvar2, @myvar3 AS myvar3 ;
@@ -214,7 +213,7 @@ GO
214
213
215
214
### C. Using a sequence with a ranking window function
216
215
217
-
```
216
+
```sql
218
217
USE AdventureWorks2012 ;
219
218
GO
220
219
@@ -227,16 +226,16 @@ GO
227
226
### D. Using the NEXT VALUE FOR function in the definition of a default constraint
228
227
Using the **NEXT VALUE FOR** function in the definition of a default constraint is supported. For an example of using **NEXT VALUE FOR** in a **CREATE TABLE** statement, see Example C[Sequence Numbers](../../relational-databases/sequence-numbers/sequence-numbers.md). The following example uses `ALTER TABLE` to add a sequence as a default to a current table.
229
228
230
-
```
229
+
```sql
231
230
CREATETABLETest.MyTable
232
231
(
233
-
IDColumn nvarchar(25) PRIMARY KEY,
234
-
name varchar(25) NOT NULL
232
+
IDColumn NVARCHAR(25) PRIMARY KEY,
233
+
name VARCHAR(25) NOT NULL
235
234
) ;
236
235
GO
237
236
238
237
CREATESEQUENCETest.CounterSeq
239
-
AS int
238
+
ASINT
240
239
START WITH 1
241
240
INCREMENT BY 1 ;
242
241
GO
@@ -259,10 +258,10 @@ GO
259
258
### E. Using the NEXT VALUE FOR function in an INSERT statement
260
259
The following example creates a table named `TestTable` and then uses the `NEXT VALUE FOR` function to insert a row.
261
260
262
-
```
261
+
```sql
263
262
CREATETABLETest.TestTable
264
-
(CounterColumn int PRIMARY KEY,
265
-
Name nvarchar(25) NOT NULL) ;
263
+
(CounterColumn INTPRIMARY KEY,
264
+
Name NVARCHAR(25) NOT NULL) ;
266
265
GO
267
266
268
267
INSERT Test.TestTable (CounterColumn,Name)
@@ -271,13 +270,12 @@ GO
271
270
272
271
SELECT*FROMTest.TestTable;
273
272
GO
274
-
275
273
```
276
274
277
275
### E. Using the NEXT VALUE FOR function with SELECT ... INTO
278
276
The following example uses the `SELECT ... INTO` statement to create a table named `Production.NewLocation` and uses the `NEXT VALUE FOR` function to number each row.
279
277
280
-
```
278
+
```sql
281
279
USE AdventureWorks2012 ;
282
280
GO
283
281
@@ -293,7 +291,7 @@ GO
293
291
### F. Granting permission to execute NEXT VALUE FOR
294
292
The following example grants **UPDATE** permission to a user named `AdventureWorks\Larry` permission to execute `NEXT VALUE FOR` using the `Test.CounterSeq` sequence.
295
293
296
-
```
294
+
```sql
297
295
GRANTUPDATEON OBJECT::Test.CounterSeq TO [AdventureWorks\Larry] ;
0 commit comments