<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'denali', 'T-SQL', 'SQL 11', and 'SQL Server 11'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=denali,T-SQL,SQL+11,SQL+Server+11&amp;orTags=0</link><description>Search results matching tags 'denali', 'T-SQL', 'SQL 11', and 'SQL Server 11'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>SQL Server v.Next (Denali) : Deriving sets using SEQUENCE</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2010/12/13/sql-server-v-next-denali-take-one-part-sequence-and-one-part-set.aspx</link><pubDate>Mon, 13 Dec 2010 12:58:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31542</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;One complaint about SEQUENCE is that there is no simple construct such as NEXT (@n) VALUES FOR so that you could get a range of SEQUENCE values as a set.&amp;nbsp; In a &lt;a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2010/11/11/sql-server-11-denali-using-sequence.aspx" title="http://sqlblog.com/blogs/aaron_bertrand/archive/2010/11/11/sql-server-11-denali-using-sequence.aspx" target="_blank"&gt;previous post about SEQUENCE&lt;/a&gt;, I mentioned that to get a range of rows, you should use the system stored procedure &lt;a href="http://msdn.microsoft.com/en-us/library/ff878352%28SQL.110%29.aspx" title="http://msdn.microsoft.com/en-us/library/ff878352%28SQL.110%29.aspx" target="_blank"&gt;sys.sp_sequence_get_range&lt;/a&gt;.&amp;nbsp; If you've tried this stored procedure, you'll know that it has a few issues:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the parameter names are not easy to memorize;&lt;/li&gt;

&lt;li&gt;it requires multiple conversions to and from SQL_VARIANT; and,&lt;br&gt;&lt;/li&gt;

&lt;li&gt;producing a set from the output requires further processing - all you get is the first and last values.&lt;br&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;SEQUENCE has a lot of limitations, and most examples thus far have shown how to use NEXT VALUE FOR to return a scalar value, but one thing you can do is use this mechanism in a limited number of query scenarios.&amp;nbsp; Let's start with a simple SEQUENCE:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;USE &lt;font color="black"&gt;tempdb;&lt;/font&gt;&lt;br&gt;GO&lt;br&gt;CREATE SEQUENCE &lt;font color="black"&gt;dbo.MySequence&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AS BIGINT&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MINVALUE &lt;font color="black"&gt;1&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; NO MAXVALUE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; START WITH &lt;font color="black"&gt;1;&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;And as a quick query example, there is nothing stopping you from doing this: &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;WITH&lt;/font&gt; fake_and_meaningless_set &lt;font color="blue"&gt;AS&lt;/font&gt;&lt;br&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="blue"&gt;SELECT&lt;/font&gt; x = &lt;font color="red"&gt;'first row'&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="blue"&gt;UNION ALL&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;/font&gt; x = &lt;font color="red"&gt;'second row'&lt;/font&gt;&lt;br&gt;)&lt;br&gt;&lt;font color="blue"&gt;SELECT &lt;font color="black"&gt;[Value] =&lt;/font&gt; NEXT VALUE FOR &lt;/font&gt;dbo.MySequence&lt;br&gt;&lt;font color="blue"&gt;FROM&lt;/font&gt; fake_and_meaningless_set;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;Results:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;Value&lt;br&gt;-----&lt;br&gt;1&lt;br&gt;2 &lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;Note that in the above case you must use UNION ALL.&amp;nbsp; If you try to use UNION, you will get the following error message, because of when the duplicates would have to be filtered out by the UNION:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="red"&gt;Msg 11721, Level 15, State 1, Line 8&lt;br&gt;NEXT VALUE FOR function cannot be used directly in a statement that uses a DISTINCT, UNION (except UNION ALL), EXCEPT or INTERSECT operator.&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;Anyway, interesting, right?&amp;nbsp; You now see that NEXT VALUE FOR can actually scale across a set.&amp;nbsp; So, given that, it is easy to see how you could construct a generic wrapper that allowed you to generate any number of sequence values as a set instead of just the first and last values.&amp;nbsp; If you need to support a max of 50 values at a time, you could use sys.objects; for 5000, you could use sys.all_columns.&amp;nbsp; For more you could implement various cross joins or you could use any of &lt;a href="http://www2.sqlblog.com/blogs/aaron_bertrand/archive/2009/10/07/bad-habits-to-kick-using-a-loop-to-populate-a-table.aspx" title="http://www2.sqlblog.com/blogs/aaron_bertrand/archive/2009/10/07/bad-habits-to-kick-using-a-loop-to-populate-a-table.aspx" target="_blank"&gt;the number generators I've described before&lt;/a&gt;.&amp;nbsp; Here is an example that assumes you won't ever need more than 5,000 sequence values to be assigned at one time:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE&lt;/font&gt; @RangeSize &lt;font color="blue"&gt;INT&lt;/font&gt; = 250;&lt;br&gt;&lt;br&gt;&lt;font color="blue"&gt;WITH&lt;/font&gt; fake_set &lt;font color="blue"&gt;AS&lt;/font&gt;&lt;br&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="blue"&gt;SELECT TOP&lt;/font&gt; (@RangeSize) column_id&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="blue"&gt;FROM&lt;/font&gt; &lt;font color="green"&gt;sys&lt;/font&gt;.&lt;font color="green"&gt;all_columns&lt;/font&gt;&lt;br&gt;)&lt;br&gt;&lt;font color="blue"&gt;SELECT&lt;/font&gt; [Value] = &lt;font color="blue"&gt;NEXT VALUE FOR&lt;/font&gt; dbo.MySequence&lt;br&gt;&lt;font color="blue"&gt;FROM&lt;/font&gt; fake_set;&lt;br&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;Results:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;Value&lt;br&gt;-----&lt;br&gt;3&lt;br&gt;4&lt;br&gt;...&lt;br&gt;251&lt;br&gt;252&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;And if you need to support more, you can do this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE&lt;/font&gt; @RangeSize &lt;font color="blue"&gt;INT&lt;/font&gt; = 50000;&lt;br&gt;&lt;br&gt;&lt;font color="blue"&gt;WITH&lt;/font&gt; fake_set &lt;font color="blue"&gt;AS&lt;/font&gt;&lt;br&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="blue"&gt;SELECT TOP&lt;/font&gt; (@RangeSize) column_id&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="blue"&gt;FROM&lt;/font&gt; &lt;font color="green"&gt;sys&lt;/font&gt;.&lt;font color="green"&gt;all_columns&lt;/font&gt; &lt;font color="blue"&gt;AS&lt;/font&gt; s1&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="blue"&gt;CROSS JOIN&lt;/font&gt; &lt;font color="green"&gt;sys&lt;/font&gt;.&lt;font color="green"&gt;all_columns&lt;/font&gt; &lt;font color="blue"&gt;AS&lt;/font&gt; s2&lt;br&gt;)&lt;br&gt;&lt;font color="blue"&gt;SELECT&lt;/font&gt; [Value] = &lt;font color="blue"&gt;NEXT VALUE FOR&lt;/font&gt; dbo.MySequence&lt;br&gt;&lt;font color="blue"&gt;FROM&lt;/font&gt; fake_set;&lt;br&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Results:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;Value&lt;br&gt;-----&lt;br&gt;253&lt;br&gt;254&lt;br&gt;...&lt;br&gt;50251&lt;br&gt;50252&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;
This runs a lot faster than you might expect.&amp;nbsp; Also note that not all of the rows will materialize; so, for example, if the cross join above yields 30 million rows, you won't use up that many SEQUENCE values - only the rows that are returned by TOP.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;You can place this code in a stored procedure, but I thought it would be more useful outside of that context because, with the T-SQL above, you could join the results with other tables or views.&amp;nbsp; Unfortunately, you can't place this code into an inline table-valued function, since a function cannot have side effects (the side effect would be assigning new sequence values).&amp;nbsp; Here is the error message you will get if you try:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="red"&gt;Msg 11719, Level 15, State 1, Procedure , Line &lt;br&gt;NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns, views, user-defined functions, user-defined aggregates, sub-queries, common table expressions, or derived tables.&lt;/font&gt;&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;So that allows you to generate a range of sequence values based on data you don't have yet (e.g. returning a set of values for an application to use).&amp;nbsp; But what about if you want to return real data with rows that are assigned by the sequence at runtime?&amp;nbsp; This is even easier.&amp;nbsp; Let's say we want to use our sequence to "tag" all objects that have a name that starts with 'sp' - we can simply do this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [ID] = &lt;font color="blue"&gt;NEXT VALUE FOR&lt;/font&gt; dbo.MySequence,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; name&lt;br&gt;&lt;font color="blue"&gt;FROM&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [master].&lt;font color="green"&gt;sys&lt;/font&gt;.&lt;font color="green"&gt;objects&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;WHERE&lt;/font&gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; name &lt;font color="gray"&gt;LIKE&lt;/font&gt; &lt;font color="red"&gt;'sp%'&lt;/font&gt;;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;That's it!&amp;nbsp; Results:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;ID &amp;nbsp;  name&lt;br&gt;----- --------------------------&lt;br&gt;50253 spt_fallback_db&lt;br&gt;50254 spt_fallback_dev&lt;br&gt;50255 spt_fallback_usg&lt;br&gt;50256 spt_monitor&lt;br&gt;50257 spt_values&lt;br&gt;50258 sp_MSrepl_startup&lt;br&gt;50259 sp_MScleanupmergepublisher&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;You'll notice these aren't in any particular order; of course not, because we did not specify an ORDER BY clause.&amp;nbsp; As has been discussed on this blog many times before, without ORDER BY, there is no default ordering, and SQL Server is free to return the results in any order it wishes.&amp;nbsp; This also holds true to the order in which it assigns SEQUENCE values.&amp;nbsp; But you will be quickly disappointed if you just add "ORDER BY name" to the above query:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="red"&gt;Msg 11723, Level 15, State 1, Line 2&lt;br&gt;NEXT VALUE FOR function cannot be used directly in a statement that contains an ORDER BY clause unless the OVER clause is specified.&lt;/font&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;This gives us a slight hint that NEXT VALUE FOR accepts an OVER clause, so that you can dictate which order the SEQUENCE values are assigned.&amp;nbsp; Sure enough (and I totally missed this when I first looked at the &lt;a href="http://msdn.microsoft.com/en-us/library/ff878370%28SQL.110%29.aspx" title="http://msdn.microsoft.com/en-us/library/ff878370%28SQL.110%29.aspx" target="_blank"&gt;NEXT VALUE FOR topic&lt;/a&gt;):&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;[ID] = &lt;font color="blue"&gt;NEXT VALUE FOR&lt;/font&gt; dbo.MySequence &lt;font color="blue"&gt;OVER&lt;/font&gt; (&lt;font color="blue"&gt;ORDER BY&lt;/font&gt; name),&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;name&lt;br&gt;&lt;font color="blue"&gt;FROM&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [master].&lt;font color="green"&gt;sys&lt;/font&gt;.&lt;font color="green"&gt;objects&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;WHERE&lt;/font&gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; name &lt;font color="gray"&gt;LIKE&lt;/font&gt; &lt;font color="red"&gt;'sp%'&lt;/font&gt;;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Results:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;ID &amp;nbsp; &amp;nbsp;name&lt;br&gt;----- --------------------------&lt;br&gt;50260 sp_MScleanupmergepublisher&lt;br&gt;50261 sp_MSrepl_startup&lt;br&gt;50262 spt_fallback_db&lt;br&gt;50263 spt_fallback_dev&lt;br&gt;50264 spt_fallback_usg&lt;br&gt;50265 spt_monitor&lt;br&gt;50266 spt_values&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;While it is true that this returns the results in the desired order even without a final ORDER BY clause, this is just a coincidence and this isn't guaranteed.&amp;nbsp; The OVER (ORDER BY) clause certainly assigned the SEQUENCE values in the order we asked for, but SQL Server is not bound to returning the rows in the exact same order every time.&amp;nbsp; So the proper form of the query should be:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;[ID] = &lt;font color="blue"&gt;NEXT VALUE FOR&lt;/font&gt; dbo.MySequence &lt;font color="blue"&gt;OVER&lt;/font&gt; (&lt;font color="blue"&gt;ORDER BY&lt;/font&gt; name),&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;name&lt;br&gt;&lt;font color="blue"&gt;FROM&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [master].&lt;font color="green"&gt;sys&lt;/font&gt;.&lt;font color="green"&gt;objects&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;WHERE&lt;/font&gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; name &lt;font color="gray"&gt;LIKE&lt;/font&gt; &lt;font color="red"&gt;'sp%'&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;ORDER BY&lt;/font&gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; name;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;Now the results are the same as the above, and you know they will be presented the same way when you run the query again tomorrow or next week. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;This technique isn't all that different from using ROW_NUMBER() without bothering with a sequence at all, except that the sequence ensures that your values are unique across the domain where you use them.&amp;nbsp; You might also be tempted to just grab the current value from sys.sequences and then add that value to a distinct ROW_NUMBER() from the set.&amp;nbsp; I strongly recommend against that, because it does not block anyone else from using conflicting values.&amp;nbsp; Note that while you can use this technique to guarantee the order of the SEQUENCE values, you still cannot assume that there will be no gaps in the event that another user is generating values at the same time.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Don't forget to clean up:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DROP SEQUENCE&lt;/font&gt; dbo.MySequence; &lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>SQL Server v.Next (Denali) : Using SEQUENCE</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2010/11/11/sql-server-11-denali-using-sequence.aspx</link><pubDate>Thu, 11 Nov 2010 18:04:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:29790</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;SQL Server 2011 introduces a feature that Oracle professionals will certainly recognize: SEQUENCE.&amp;nbsp; SEQUENCE allows you to define a central place where SQL Server will maintain a counter of values for you.&amp;nbsp; The last-used value is stored in memory and so no storage is required.&amp;nbsp; Here is an example:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE SEQUENCE&lt;/font&gt;&lt;font color="black"&gt; dbo.OrderIDs&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS INT&lt;br&gt;    MINVALUE&lt;/font&gt;&lt;font color="black"&gt; 1&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;NO MAXVALUE&lt;br&gt;    START&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="blue"&gt;WITH &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET NOCOUNT ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;NextOrderID = &lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;UNION &lt;/font&gt;&lt;font color="gray"&gt;ALL &lt;/font&gt;&lt;font color="blue"&gt;    SELECT &lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;UNION &lt;/font&gt;&lt;font color="gray"&gt;ALL     &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR&lt;/font&gt;&lt;font color="blue"&gt; &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs;&lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Results:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;NextOrderID&lt;br&gt;-----------&lt;br&gt;1&lt;br&gt;2&lt;br&gt;3&lt;br&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Notice I did not have to create any tables or worry about persisting information to disk in any way.&amp;nbsp; What about reseeding?&amp;nbsp; This is a common problem with IDENTITY columns.&amp;nbsp; You can "reseed" a sequence using ALTER and RESTART WITH: &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;ALTER SEQUENCE&lt;/font&gt;&lt;font color="black"&gt; dbo.foo&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;    RESTART &lt;/font&gt;&lt;/font&gt;&lt;font color="blue"&gt;WITH &lt;font color="black"&gt;20&lt;/font&gt;&lt;/font&gt;&lt;font color="blue"&gt;;&lt;br&gt;&lt;br&gt;SET NOCOUNT ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;NextOrderID = &lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;UNION &lt;/font&gt;&lt;font color="gray"&gt;ALL &lt;/font&gt;&lt;font color="blue"&gt;    SELECT &lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;UNION &lt;/font&gt;&lt;font color="gray"&gt;ALL     &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR&lt;/font&gt;&lt;font color="blue"&gt; &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs;&lt;/font&gt; &lt;br&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;Results:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;NextOrderID&lt;br&gt;-----------&lt;br&gt;20&lt;br&gt;21&lt;br&gt;22&lt;br&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;And if you want to roll back the clock or re-use an existing sequence, you can use a lower number with RESTART WITH as well - though I'd be careful with this depending on how you are using the values. &lt;/p&gt;

&lt;p&gt;Want to increment by a value other than 1 (the default)?&amp;nbsp; You can use INCREMENT BY:&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;ALTER SEQUENCE&lt;/font&gt;&lt;font color="black"&gt; dbo.foo&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;    RESTART &lt;/font&gt;&lt;/font&gt;&lt;font color="blue"&gt;WITH &lt;/font&gt;&lt;font color="blue"&gt;&lt;font&gt;&lt;font color="black"&gt;1&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font&gt;&lt;font&gt;&lt;font&gt;&lt;font color="blue"&gt;    INCREMENT BY &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="black"&gt;5&lt;/font&gt;&lt;/font&gt;&lt;font color="blue"&gt;;&lt;br&gt;&lt;br&gt;SET NOCOUNT ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;NextOrderID = &lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;UNION &lt;/font&gt;&lt;font color="gray"&gt;ALL &lt;/font&gt;&lt;font color="blue"&gt;    SELECT &lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;UNION &lt;/font&gt;&lt;font color="gray"&gt;ALL     &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="blue"&gt;NEXT VALUE FOR&lt;/font&gt;&lt;font color="blue"&gt; &lt;/font&gt;&lt;font color="black"&gt;dbo.OrderIDs;&lt;/font&gt; &lt;br&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;Results:&lt;/p&gt;


&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;NextOrderID&lt;br&gt;-----------&lt;br&gt;1&lt;br&gt;6&lt;br&gt;11&lt;br&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;You can start a sequence at a value greater than 1 simply by changing the MINVALUE.&amp;nbsp; You can also start at the low end of the data range, e.g. to use all 4 billion values for INT instead of just the 2 billion positive values. And you can even use INCREMENT BY to count down instead of up; just use a negative value.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;You can also ensure that the sequence doesn't use all the possible values, for example if you wanted to have a maximum of 20 values in a TINYINT sequence, you could use the following code to produce a max of 20 values instead of all 255: &lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE SEQUENCE&lt;/font&gt;&lt;font color="black"&gt; dbo.foo&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS TINYINT&lt;br&gt;    MINVALUE&lt;/font&gt;&lt;font color="black"&gt; 1&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;MAXVALUE&lt;font color="black"&gt; 20&lt;/font&gt;&lt;br&gt;    START &lt;/font&gt;&lt;/font&gt;&lt;font color="blue"&gt;WITH &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;font color="black"&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;
When you create such a sequence, you will get the following warning message (note that it is not an error):&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;The sequence object 'foo' cache size is greater than the number of available values; the cache size has been automatically set to accommodate the remaining sequence values.&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;And if you actually try to go past the first 20 values:  &lt;br&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT NEXT VALUE FOR&lt;/font&gt; dbo.foo;&lt;br&gt;&lt;font color="blue"&gt;GO&lt;/font&gt; 21&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;You will get this error message:
&lt;/p&gt;


&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;color:red;"&gt;Msg 11728, Level 16, State 1, Line 2&lt;br&gt;The sequence object 'foo' has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;You can restart the sequence using ALTER SEQUENCE ... RESTART WITH.&amp;nbsp; If you want to use up all of the values and then loop around and start over, you can use the CYCLE option. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;Surprisingly, if you simply overflow the datatype, you get the same sequence-related error message as opposed to the more familiar Msg 220 (arithmetic overflow) error: &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT NEXT VALUE FOR&lt;/font&gt; dbo.foo;&lt;br&gt;&lt;font color="blue"&gt;GO&lt;/font&gt; 256&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;It is important to note that SEQUENCE will not provide you a transactionally consistent and gap-free stream of values.&amp;nbsp; For example, just like IDENTITY, if your transaction rolls back, the next value is still taken: &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE SEQUENCE&lt;/font&gt; dbo.bar&lt;br&gt;&lt;font color="blue"&gt;    AS INT&lt;br&gt;    MINVALUE &lt;/font&gt;1&lt;font color="blue"&gt;&lt;br&gt;    NO MAXVALUE &lt;br&gt;    START WITH &lt;/font&gt;1;&lt;br&gt;&lt;font color="blue"&gt;GO&lt;br&gt;&lt;br&gt;BEGIN TRANSACTION&lt;/font&gt;;&lt;font color="blue"&gt;&lt;br&gt;    SELECT&lt;/font&gt; NextBar = &lt;font color="blue"&gt;NEXT VALUE FOR &lt;/font&gt;dbo.bar;&lt;br&gt;&lt;font color="blue"&gt;ROLLBACK TRANSACTION&lt;/font&gt;;&lt;br&gt;&lt;font color="blue"&gt;SELECT&lt;/font&gt; NextBar = &lt;font color="blue"&gt;NEXT VALUE FOR &lt;/font&gt;dbo.bar;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Results:
&lt;/p&gt;


&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;NextBar&lt;br&gt;-----------&lt;br&gt;1&lt;br&gt;&lt;br&gt;NextBar&lt;br&gt;-----------&lt;br&gt;2&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;And if you want to check the current value of the sequence without actually using up a value (like checking IDENT_CURRENT() or DBCC CHECKIDENT), you can check the new catalog view, sys.sequences:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="#3300ff"&gt;SELECT&lt;/font&gt; current_value &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#3300ff"&gt;FROM&lt;/font&gt; &lt;font color="#009900"&gt;sys&lt;/font&gt;.&lt;font color="#009900"&gt;sequences&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#3300ff"&gt;WHERE&lt;/font&gt; name = &lt;font color="#ff0000"&gt;'OrderIDs'&lt;/font&gt;;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can also check several other properties of the sequence this way (don't bother trying to use sp_help - all it will tell you is that it is a sequence object). &lt;/p&gt;

&lt;p&gt;Want to get a range of SEQUENCE values?&amp;nbsp; Don't use a loop; there's a stored procedure for that: sys.sp_sequence_get_range.&amp;nbsp; Here is a sample:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="#3300ff"&gt;DECLARE&lt;/font&gt; @fv &lt;font color="#3300ff"&gt;SQL_VARIANT&lt;/font&gt;, @lv &lt;font color="#3300ff"&gt;SQL_VARIANT&lt;/font&gt;;&lt;br&gt;&lt;br&gt;&lt;font color="#3300ff"&gt;EXEC&lt;/font&gt; &lt;font color="#006600"&gt;sys&lt;/font&gt;.&lt;font color="#006600"&gt;sp_sequence_get_range&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @sequence_name     = &lt;font color="#ff0000"&gt;'OrderIDs'&lt;/font&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @range_size        = 20,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @range_first_value = @fv &lt;font color="#3300ff"&gt;OUTPUT&lt;/font&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @range_last_value  = @lv &lt;font color="#3300ff"&gt;OUTPUT&lt;/font&gt;;&lt;br&gt;&lt;br&gt;&lt;font color="#3300ff"&gt;SELECT&lt;/font&gt; &lt;br&gt;    fv = &lt;font color="#cc00ff"&gt;CONVERT&lt;/font&gt;(&lt;font color="#3300ff"&gt;INT&lt;/font&gt;, @fv), &lt;br&gt;    lv = &lt;font color="#ff00ff"&gt;CONVERT&lt;/font&gt;(&lt;font color="#3300ff"&gt;INT&lt;/font&gt;, @lv),&lt;br&gt;    next = &lt;font color="#3300ff"&gt;NEXT VALUE FOR&lt;/font&gt; dbo.OrderIDs; &lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Results:&lt;/p&gt;
&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;fv   lv   next&lt;br&gt;---- ---- ----&lt;br&gt;&amp;nbsp;5     24   25&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;b&gt;&lt;font size="4"&gt;But how does it perform?&lt;/font&gt;&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I can see significant performance improvements over using a central identity table or a custom increment table (two typical ways this feature has been implemented).&amp;nbsp; In the following example, I show five different ways to manage a central value generator.&amp;nbsp; The first is a custom increment table, a popular method which uses a quirky update and is certainly far from standard.&amp;nbsp; Next there are two methods using an IDENTITY column: one where the old rows are deleted before the next value is generated (for no other reason other than to save space), and the other that doesn't bother to do the delete, letting the table grow as new values are generated.&amp;nbsp; Finally, we use the sequence, once with a procedure wrapper to simplify the changes to existing scripts, and once with the new NEXT VALUE syntax inserted into the code:&lt;/p&gt;


&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;USE &lt;font color="#000000"&gt;[master]&lt;/font&gt;&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;&lt;br&gt;&lt;font color="#009900"&gt;-- drop the database if it exists, and the #timing temp table&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- then create the database&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;IF &lt;/font&gt;&lt;font color="magenta"&gt;DB_ID&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="red"&gt;'BookStore'&lt;/font&gt;&lt;font color="gray"&gt;) &amp;gt; &lt;/font&gt;&lt;font color="black"&gt;0&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp; DROP DATABASE &lt;/font&gt;&lt;font color="black"&gt;[BookStore]&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;IF &lt;/font&gt;&lt;font color="magenta"&gt;OBJECT_ID&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="red"&gt;'tempdb..#timing'&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;IS &lt;/font&gt;&lt;font color="gray"&gt;NOT NULL&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp; DROP TABLE &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;CREATE DATABASE &lt;/font&gt;&lt;font color="black"&gt;[BookStore]&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;USE &lt;/font&gt;&lt;font color="black"&gt;[BookStore]&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET NOCOUNT ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- create a table with an IDENTITY column&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;CREATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralIdentity&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;ID &lt;font color="#3300ff"&gt;BIGINT &lt;/font&gt;&lt;/font&gt;&lt;font color="#3300ff"&gt;IDENTITY&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;PRIMARY KEY&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="blue"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- create a "roll your own" increment table and seed initial value&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;&lt;br&gt;CREATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralIncrement&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;ID &lt;font color="#3300ff"&gt;BIGINT&lt;/font&gt; &lt;/font&gt;&lt;font color="blue"&gt;PRIMARY KEY&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralIncrement&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;ID&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;0&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- create a sequence with the same seed and increment properties&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;CREATE SEQUENCE&lt;/font&gt;&lt;font color="black"&gt; dbo.CentralSequence&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS BIGINT&lt;br&gt;    MINVALUE 1&lt;br&gt;    NO MAXVALUE&lt;br&gt;    START&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="blue"&gt;WITH &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- stored procedure for increment, which uses quirky update&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;CREATE PROCEDURE &lt;/font&gt;&lt;font color="black"&gt;dbo.Central_CreateIncrement&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="black"&gt;&lt;font color="#3300ff"&gt;BIGINT OUTPUT&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp; SET NOCOUNT ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;UPDATE &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralIncrement &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SET &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;ID &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;ID &lt;/font&gt;&lt;font color="gray"&gt;+ &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- stored procedure for increasing the IDENTITY column&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- with a parameter for deleting the existing value(s) first&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;CREATE PROCEDURE &lt;/font&gt;&lt;font color="black"&gt;dbo.Central_CreateIdentity&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;BIGINT OUTPUT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@Delete &lt;/font&gt;&lt;font color="black"&gt;&lt;font color="#3300ff"&gt;BIT&lt;/font&gt; &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;0&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp; SET NOCOUNT ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;IF &lt;/font&gt;&lt;font color="#434343"&gt;@Delete &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;1&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DELETE &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralIdentity&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; INSERT &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralIdentity &lt;/font&gt;&lt;font color="blue"&gt;DEFAULT VALUES&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="magenta"&gt;SCOPE_IDENTITY&lt;/font&gt;&lt;font color="gray"&gt;();&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- stored procedure for handling incrementing the SEQUENCE&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;CREATE PROCEDURE &lt;/font&gt;&lt;font color="black"&gt;dbo.Central_CreateSequence&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="black"&gt;&lt;font color="#3300ff"&gt;BIGINT OUTPUT&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp; SET NOCOUNT ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;&lt;font color="#3300ff"&gt;NEXT VALUE&lt;/font&gt; &lt;/font&gt;&lt;font color="blue"&gt;FOR &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralSequence&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- simple table that will be the recipient of key values&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;CREATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;OrderID &lt;/font&gt;&lt;font color="blue"&gt;INT PRIMARY KEY&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;);&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- simple table for tracking timings&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;CREATE TABLE &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;Step&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;64&lt;/font&gt;&lt;font color="gray"&gt;),&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;dt&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color="#3300ff"&gt;DATETIME2&lt;/font&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;7&lt;/font&gt;&lt;font color="gray"&gt;) NOT NULL &lt;/font&gt;&lt;font color="blue"&gt;DEFAULT &lt;/font&gt;&lt;font color="#ff00ff"&gt;SYSUTCDATETIME&lt;/font&gt;&lt;font color="gray"&gt;()&lt;br&gt;);&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- start each step with a blank orders table&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- this step is handling the quirky increment update&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;TRUNCATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Starting increment'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;BIGINT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;EXEC &lt;/font&gt;&lt;font color="black"&gt;dbo.Central_CreateIncrement&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;OUTPUT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;OrderID&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="#434343"&gt;@NextID&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; 100000&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Finished increment'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- this step is handling the identity with the delete&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;TRUNCATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Starting identity + delete'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;BIGINT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;EXEC &lt;/font&gt;&lt;font color="black"&gt;dbo.Central_CreateIdentity&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;OUTPUT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@Delete &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;OrderID&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="#434343"&gt;@NextID&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; 100000&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Finished identity + delete'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- this step is handling the identity without the delete&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;TRUNCATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Starting identity without delete'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;BIGINT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;EXEC &lt;/font&gt;&lt;font color="black"&gt;dbo.Central_CreateIdentity&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;OUTPUT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@Delete &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;OrderID&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="#434343"&gt;@NextID&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; 100000&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Finished identity without delete'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- this step handles the SEQUENCE with the procedure wrapper&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;TRUNCATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Starting sequence with PROC'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;BIGINT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;EXEC &lt;/font&gt;&lt;font color="black"&gt;dbo.Central_CreateSequence&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="#434343"&gt;@NextID &lt;/font&gt;&lt;font color="#3300ff"&gt;OUTPUT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;OrderID&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="#434343"&gt;@NextID&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; 100000&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Finished sequence with PROC'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- this step handles the SEQUENCE directly&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;TRUNCATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Starting sequence without PROC'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;OrderID&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT NEXT VALUE &lt;/font&gt;&lt;font color="blue"&gt;FOR &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralSequence&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; 100000&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'Finished sequence without PROC'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;&lt;font color="#009900"&gt;-- ok, now we can run a query to measure timings&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;font color="blue"&gt;WITH &lt;/font&gt;&lt;font color="black"&gt;t &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;Step&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;dt&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;rn &lt;/font&gt;&lt;font color="blue"&gt;= &lt;font color="#ff00ff"&gt;ROW_NUMBER&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt;&lt;/font&gt;&lt;font color="gray"&gt;() &lt;/font&gt;&lt;font color="blue"&gt;OVER &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="blue"&gt;ORDER BY &lt;/font&gt;&lt;font color="black"&gt;dt&lt;/font&gt;&lt;font color="gray"&gt;) &lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;FROM&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;#timing&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;t1.Step&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;dur_s&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="magenta"&gt;DATEDIFF&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#3300ff"&gt;SECOND&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;t1.dt&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;t2.dt&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM &lt;/font&gt;&lt;font color="black"&gt;t &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;t1&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INNER JOIN &lt;/font&gt;&lt;font color="black"&gt;t &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;t2&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;ON &lt;/font&gt;&lt;font color="black"&gt;t1.rn &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;t2.rn &lt;/font&gt;&lt;font color="gray"&gt;- &lt;/font&gt;&lt;font color="black"&gt;1&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;WHERE &lt;/font&gt;&lt;font color="black"&gt;t1.rn &lt;/font&gt;&lt;font color="gray"&gt;% &lt;/font&gt;&lt;font color="black"&gt;2 &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;1&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;AND &lt;/font&gt;&lt;font color="black"&gt;t2.rn &lt;/font&gt;&lt;font color="gray"&gt;% &lt;/font&gt;&lt;font color="black"&gt;2 &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;0&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;The results, keeping in mind that this was in a VM on a laptop:&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;&lt;img src="http://sqlblog.com/files/folders/30269/download.aspx" height="332" width="616"&gt;&lt;br&gt;&lt;/blockquote&gt;

&lt;p&gt;The clear winner is obviously the SEQUENCE operator, since it doesn't have to write the data to disk in order to persist the current value.&amp;nbsp; Next I ran the same tests, but this time turned page compression on for the increment and identity tables, to see if the reduced I/O might let the existing solutions come a little closer.&amp;nbsp; The only change to the above script is in bold:
&lt;/p&gt;


&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralIdentity&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;ID &lt;font color="#3300ff"&gt;BIGINT &lt;/font&gt;&lt;/font&gt;&lt;font color="#3300ff"&gt;IDENTITY&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;PRIMARY KEY&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&lt;/font&gt;&lt;font color="#000000"&gt;&lt;b&gt;WITH (DATA_COMPRESSION &lt;font&gt;&lt;font&gt;&lt;font&gt;=&lt;/font&gt;&lt;/font&gt;&lt;/font&gt; PAGE&lt;/b&gt;&lt;/font&gt;&lt;font color="gray"&gt;&lt;font color="#000000"&gt;&lt;b&gt;)&lt;/b&gt;;&lt;/font&gt;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;CREATE TABLE &lt;/font&gt;&lt;font color="black"&gt;dbo.CentralIncrement&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;ID &lt;font color="#3300ff"&gt;BIGINT&lt;/font&gt; &lt;/font&gt;&lt;font color="blue"&gt;PRIMARY KEY&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&lt;/font&gt;&lt;font color="#000000"&gt;&lt;b&gt;WITH (DATA_COMPRESSION &lt;font&gt;=&lt;/font&gt; PAGE&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;&lt;b&gt;)&lt;/b&gt;;&lt;/font&gt;&lt;font color="blue"&gt;&lt;br&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;The results were indistinguishable.&amp;nbsp; In fact, from run to run, one method would gain a second, and another would lose two; on the next run, the first one would lose three seconds and another would gain one.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;I also tried putting a NEXT VALUE FOR command in a user-defined function, to see if there was an easier way to inline the syntax, and see if the UDF overhead would impact the observed benefit.&amp;nbsp; However if you try to create a function with a reference to a sequence object, here is the error you will get (which will also point out a few other places where you shouldn't bother trying to use them - it's a pretty long laundry list):&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;color:red;"&gt;Msg 11719, Level 15, State 1, Procedure Central_CreateSequence, Line 6&lt;br&gt;NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns, views, user-defined functions, user-defined aggregates, sub-queries, common table expressions, or derived tables.&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note also that you can use NEXT VALUE FOR in a UNION ALL, but not in a UNION, EXCEPT, INTERSECT, or with DISTINCT:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;color:red;"&gt;Msg 11721, Level 15, State 1, Line 9&lt;br&gt;NEXT VALUE FOR function cannot be used directly in a statement that uses a DISTINCT, UNION (except UNION ALL), EXCEPT or INTERSECT operator.&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;
If you look in sys.messages, you'll see there are several other limitations in the initial implementation of SEQUENCE. Some of these error messages are quite logical, but others are quite interesting.&amp;nbsp; For example, it is okay to reference a sequence object in another database, but not when defined as a default constraint. &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table style="border-collapse:collapse;" cellpadding="5" cellspacing="0"&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11700&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The increment for sequence object '%.*ls' cannot be zero.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11701&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The absolute value of the increment for sequence object '%.*ls' must be less than or equal to the difference between the minimum and maximum value of the sequence object.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11702&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The sequence object '%.*ls' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, or any user-defined data type that is based on one of the above integer data types.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11703&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The start value for sequence object '%.*ls' must be between the minimum and maximum value of the sequence object.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11704&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The current value '%.*ls' for sequence object '%.*ls' must be between the minimum and maximum value of the sequence object.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11705&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The minimum value for sequence object '%.*ls' must be less than its maximum value.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11706&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The cache size for sequence object '%.*ls' must be greater than 0.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11707&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The cache size for sequence object '%.*ls' has been set to NO CACHE.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11709&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The 'RESTART WITH' argument cannot be used in a CREATE SEQUENCE statement.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11710&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;Argument 'START WITH' cannot be used in an ALTER SEQUENCE statement.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11711&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;Argument 'AS' cannot be used in an ALTER SEQUENCE statement.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11714&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;Invalid sequence name '%.*ls'.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11715&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;No properties specified for ALTER SEQUENCE.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11716&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR function does not support the PARTITION BY clause.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11717&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR function does not support the OVER clause in default constraints, UPDATE statements, or MERGE statements.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11718&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR function does not support an empty OVER clause.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11719&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns, views, user-defined functions, user-defined aggregates, sub-queries, common table expressions, or derived tables.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11720&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR function is not allowed in the TOP, OVER, OUTPUT, ON, WHERE, GROUP BY, HAVING, ORDER BY, COMPUTE, or COMPUTE BY clauses.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11721&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR function cannot be used directly in a statement that uses a DISTINCT, UNION (except UNION ALL), EXCEPT or INTERSECT operator.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11722&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR function is not allowed in the WHEN MATCHED clause, the WHEN NOT MATCHED clause, or the WHEN NOT MATCHED BY SOURCE clause of a merge statement.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11723&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR function cannot be used directly in a statement that contains an ORDER BY clause unless the OVER clause is specified.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11724&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;An expression that contains a NEXT VALUE FOR function cannot be passed as an argument to a table-valued function.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11725&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;An expression that contains a NEXT VALUE FOR function cannot be passed as an argument to an aggregate.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11726&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;Object '%.*ls' is not a sequence object.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11727&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;NEXT VALUE FOR functions for a given sequence object must have exactly the same OVER clause definition.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11728&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The sequence object '%.*ls' has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11729&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The sequence object '%.*ls' cache size is greater than the number of available values; the cache size has been automatically set to accommodate the remaining sequence values.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11730&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;Database name cannot be specified for the sequence object in default constraints.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11731&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;A column that uses a sequence object in the default constraint must be present in the target columns list, if the same sequence object appears in either a row constructor or under UNION ALL.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="border:1px solid black;"&gt;Msg 11732&lt;/td&gt;

&lt;td style="border:1px solid black;"&gt;The requested range for sequence object '%.*ls' exceeds the maximum or minimum limit. Retry with a smaller range.&lt;/td&gt;
&lt;/tr&gt;

&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Hope that gives a good taste of SEQUENCE.&amp;nbsp; As you start contemplating the move to the next version of SQL Server, you may want to consider this feature if you are currently using a central identity or increment table - as long as you can get along with its limitations.&amp;nbsp; If you want more official information on SEQUENCE, including a great description on how memory is used to cache sequence values and avoid I/O, you can start at this Books Online topic, &lt;a href="http://msdn.microsoft.com/en-us/library/ff878058%28v=SQL.110%29.aspx" title="http://msdn.microsoft.com/en-us/library/ff878058(v=SQL.110).aspx" target="_blank"&gt;Creating and Using Sequence Numbers&lt;/a&gt;.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp; &lt;br&gt;&lt;/p&gt;</description></item></channel></rss>