Labels

Wednesday, April 3, 2013

Sequence


/*
The new SEQUENCE type in SQL Server 2012 will allows you to create sequential numbers.
In general identity columns can be used to uniquely identify records in a table.
But if we wants to maintain identity columns across multiple tables, or if we need the identity column before insertion of
record into table, we can use SEQUENCE type in SQL Server 2012.
*/

USE ExploringSQL
GO

CREATE SEQUENCE CustomSequence AS INT
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 10000;
GO

CREATE TABLE TEST1
(
Id int,
Col1 varchar(100)
)
GO

CREATE TABLE TEST2
(
ID int,
Col2 varchar(100)
)
GO

INSERT INTO TEST1 (Id,Col1)
VALUES (NEXT VALUE FOR CustomSequence,'a')
INSERT INTO TEST2 (Id,Col2)
VALUES (NEXT VALUE FOR CustomSequence,'b')

SELECT * From Test1
SELECT * From Test2

DROP TABLE TEST1
DROP TABLE TEST2

No comments:

Post a Comment