Sequences in SQL Server 2012

Sequences where for Oracle until now 😉

Now, SQL Server 2012 comes with a nice and simple to use Sequence Feature like Oracle.

An example sequence ‘countToten’ which would have sequence from 1 to 10 could be created like this :

CREATE SEQUENCE countToten
   AS tinyint
    START WITH 1
    INCREMENT BY 1
    MINVALUE 1
    MAXVALUE 10
    CYCLE ;
GO

To reset the sequence to 1 you can use the following query:

ALTER SEQUENCE countToten
 RESTART WITH 1 ;

Finally, if it is necessary to drop the sequence, you can use the DROP SEQUENCE

drop sequence countToten

Winding up with an example :

Create a table with one int column, and insert data from the sequence.

CREATE TABLE seqTest
(column1 INT)
GO
INSERT seqTest(column1) values
(NEXT Value for countToten),
(NEXT Value for countToten),
(NEXT Value for countToten),
(NEXT Value for countToten),
(NEXT Value for countToten)

😉 Happy Sql Coding !

Advertisements

About mytechlifedays

Its been a wonderful learning period over the last 6 years in the IT industry , getting exposed to whole lot of technologies and ideas. The hurdles and the crisis that came along have been wonderful experience ... And now its time to pen them down so that let some others execute faster and easily with these information .....
This entry was posted in Database and tagged , . Bookmark the permalink.

One Response to Sequences in SQL Server 2012

  1. Pingback: Understanding Sequences and their use – SQL Server 2012 | My Tech Days

Leave a Reply ! It would be always appreciated ! :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s