• Skip to main content
  • Skip to primary sidebar

DBA Diaries

Thoughts and experiences of a DBA working with SQL Server and MySQL

New T-SQL features in SQL Server 2012 – CREATE SEQUENCE

February 9, 2012 by Andy Hayes Leave a Comment

Continuing on from new T-SQL features of SQL Server 2012, I wanted to explain about the new CREATE SEQUENCE feature.

What is CREATE SEQUENCE for?

A common feature in table design is to place an auto incrementing number on a field in the form of an IDENTITY column. So this is an easy way of maintaining a sequential number on a table.

What if you wanted to create a database wide identity? Prior to SQL 2012, you might choose to do it by having a table sitting there in the middle of it all with a numeric field which gets updated by some function every time a new identity is created. CREATE SEQUENCE takes away this overhead.

You can create a sequence either using SQL Server Management Studio or using T-SQL. In Management Studio, find the “Sequences” folder under “Programmability” beneath the database you want to add a sequence for.

create sequence

Here you can configure the sequence according to your design.

To do the same thing in T-SQL, the syntax is:
[sourcecode language=’sql’]CREATE SEQUENCE MyDemoSequence
START WITH 1
INCREMENT BY 1;[/sourcecode]
If you choose to do this using T-SQL then by default a sequence is created as a BIGINT datatype unless you specify otherwise, for example:
[sourcecode language=’sql’]CREATE SEQUENCE MyDemoSequence AS SMALLINT
START WITH 1
INCREMENT BY 1;[/sourcecode]
Note that START, INCREMENT, MINVALUE and MAXVALUE must be configured within the boundaries of the data type. For example, you couldn’t specify a negative START value for a TINYINT data type.

To use CREATE SEQUENCE

To get the next value in the sequence use the NEXT VALUE FOR
[sourcecode language=’sql’]SELECT NEXT VALUE FOR MyDemoSequence;[/sourcecode]
If you were inserting to a table, it would like something like this:
[sourcecode language=’sql’]INSERT INTO YourTable(ID, Name)
VALUES(NEXT VALUE FOR MyDemoSequence, ‘Your name’);[/sourcecode]

So what else is there to know about CREATE SEQUENCE?

CYCLE and NO CYCLE tells the sequence to cycle back round to the minimum value when the last value has been allocated in the sequence. By default, this is set to NO CYCLE.

CACHE or NO CACHE, designed for performance benefits in reducing IO requests for new numbers out of a sequence. A cache value can be specified and the numbers up to the maximum cache value are loaded up into memory until the cache is exceeded and a new set of numbers is required.

For example, you might create a sequence with a cache of 20. When a value is needed from the sequence, the minimum value in the sequence up to and including the CACHE value are loaded into memory. The CACHE value of 20 is written to the system table sys.sequences and when 20 gets used and 21 is requested, then a fresh set of numbers is allocated to the cache (21-40) with 40 being written to the sys.sequences table as the CACHE value.

Here’s an example of a sequence created using CYCLE and CACHE
[sourcecode language=’sql’]CREATE SEQUENCE MyDemoSequence AS INT
START WITH 1
INCREMENT BY 1
CACHE 20
CYCLE;[/sourcecode]
Lets have a quick look at the sys.sequences table
[sourcecode language=’sql’]SELECT cache_size, is_cached, current_value
FROM sys.sequences
WHERE name = ‘MyDemoSequence’;[/sourcecode]
Returns

cache_size  is_cached current_value
----------- --------- -------------
20          1         1

(1 row(s) affected)

 

Related Posts:

  • sql grouping sets
    Using SQL GROUPING SETS for Multiple GROUP BY…
  • How to fix "conversion failed when converting date and/or time from character string"
    How to fix "conversion failed when converting date…

Filed Under: All Articles, SQL Tips and Tricks Tagged With: sql server 2012, t-sql

About Andy Hayes

Andy Hayes is a DBA working with SQL Server since version 7.0. He has a wonderful wife and two beautiful children. He loves database technology, playing cricket, and blogging. He is passionate about sharing his experiences as a DBA and learning more to further his understanding and knowledge. You can follow me on Twitter, check out my Facebook page or follow me on Google+

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Categories

  • All Articles (84)
  • Career Development (8)
  • MySQL Administration (18)
  • MySQL Performance (2)
  • SQL Server Administration (24)
  • SQL Server News (3)
  • SQL Server Performance (14)
  • SQL Server Security (3)
  • SQL Tips and Tricks (21)

Top 10 Popular Posts

  • Using sp_change_users_login to fix SQL Server orphaned users
  • MySQL SHOW USERS? – How to List All MySQL Users and Privileges
  • How to shrink tempdb
  • How to Transfer Logins to Another SQL Server or Instance
  • How to Delete Millions of Rows using T-SQL with Reduced Impact
  • T-SQL – How to Select Top N Rows for Each Group Using ROW_NUMBER()
  • New T-SQL features in SQL Server 2012 – OFFSET and FETCH
  • How to Kill All MySQL Processes For a Specific User
  • Using exec sp_who2 to help with SQL Server troubleshooting
  • How to fix “conversion failed when converting date and/or time from character string”

Recent Posts

  • How to fix “conversion failed when converting date and/or time from character string”
  • Using SQL GROUPING SETS for Multiple GROUP BY Queries in a Single Query
  • How to Setup MySQL Master Master Replication
  • How To Use SQL to Convert a STRING to an INT
  • How to set up MySQL Replication Tutorial

Search

Connect

  • Twitter
  • Facebook
  • RSS

About

  • Cookie Policy
  • Disclaimer
  • About
Copyright ©