CREATE SEQUENCE dbo.seq_test AS INT
START WITH 1
INCREMENT BY 1;
The statement above will create a sequence seq_test. Now In order to retrieve sequential values from the sequence use the following statement:
SELECT NEXT VALUE FOR dbo.seq_test. This would return the value 1 since the starting value is set to 1. When the select statement is run again the value will be 2.
Now If I were use this sequence in a insert statement where in one of the values is an incremental value I could do the following: Let us assume we have a table called Employee which a id column and a name column.
INSERT INTO dbo.Employee
VALUES(NEXT VALUE FOR dbo.seq_test,'James');
In Summary this is a useful feature which can be utilised by developers in situations where a incremental type value is needed.
Post a Comment