﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by ron.carpenter  / What identity comes next? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 09:27:34 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>So, can we say that what SQL Server does to determine the next identityvalue is:[code="sql"]Select Sign(Ident_Incr('tablename')) * Max(Sign(Ident_Incr('tablename')) * $IDENTITY) + Ident_Incr('tablename')From tablename[/code]where [i]$IDENTITY[/i] is the identity column of table [i]tablename[/i] ?</description><pubDate>Wed, 29 Sep 2010 16:35:47 GMT</pubDate><dc:creator>Melsen Asllani</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>I had to puzzle on this one for a while.  Thanks.</description><pubDate>Wed, 02 Jun 2010 07:00:43 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Another great question, I had to think about this one, but it makes sense.  Thanks!</description><pubDate>Tue, 01 Jun 2010 20:26:16 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Thanks for the question.</description><pubDate>Tue, 01 Jun 2010 10:31:39 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>I had some trouble understanding the explanation of the correct answer at first. The explanation for the answer states, "SQL Server will use the maximum identity value + increment in the direction of the increment for the next identity." I'm not sure if there is any better way to word it. But I first thought "maximum identity value" was intended to mean 100 in example 2 (for example, SELECT MAX(Temp2ID) FROM #Temp2). Then I realized that the intended meaning was something like, "the value farthest in the direction of the increment value's sign." Or, in terms of SQL: MAX(ID) + 1 if the increment is &amp;gt;= 1, MIN(ID) - 1, if the increment is &amp;lt;= -1.Here is the sample code with two additional queries I wrote to help me understand it.[code]CREATE TABLE #Temp1( Temp1ID int NOT NULL IDENTITY (-2147483648,1), Temp1Value char(1) NOT NULL) ;CREATE TABLE #Temp2( Temp2ID int NOT NULL IDENTITY (-1,-1), Temp2Value char(1) NOT NULL) ;INSERT INTO #Temp1 (Temp1Value) VALUES ('1') ;INSERT INTO #Temp1 (Temp1Value) VALUES ('2') ;SET IDENTITY_INSERT #Temp1 ON ;INSERT INTO #Temp1 (Temp1ID,Temp1Value) VALUES (100,'3') ;SET IDENTITY_INSERT #Temp1 OFF ;-- Query to show current and next increment values.SELECT MAX(Temp1ID) AS CURRENT_INCREMENT_VALUE, MAX(Temp1ID) + 1 AS NEXT_INCREMENT_VALUE FROM #Temp1;INSERT INTO #Temp1 (Temp1Value) VALUES ('4') ;INSERT INTO #Temp2 (Temp2Value) VALUES ('1') ;INSERT INTO #Temp2 (Temp2Value) VALUES ('2') ;SET IDENTITY_INSERT #Temp2 ON ;INSERT INTO #Temp2 (Temp2ID,Temp2Value) VALUES (100,'3') ;-- Query to show current and next increment values.-- Used adding a negative integer to demonstrate "increment" of -1.SELECT MIN(Temp2ID) AS CURRENT_INCREMENT_VALUE, MIN(Temp2ID) + (-1) AS NEXT_INCREMENT_VALUE FROM #Temp2;SET IDENTITY_INSERT #Temp2 OFF ;INSERT INTO #Temp2 (Temp2Value) VALUES ('4') ;SELECT Temp1ID,Temp2IDFROM #Temp1 t1INNER JOIN #Temp2 t2 ON t1.Temp1Value = t2.Temp2ValueWHERE t1.Temp1Value = '4' ;DROP TABLE #Temp1 ;DROP TABLE #Temp2 ;[/code]But it is good that SQL Server doesn't work in such a way as to make the wrong answer that I picked (101,99) correct. :w00t: Otherwise, as others have noted, you would end up with ID collisions when using negative increments.Thanks,webrunner</description><pubDate>Tue, 01 Jun 2010 09:01:57 GMT</pubDate><dc:creator>webrunner</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Tricky. I had a 50-50 chance and blew it!Good question. Thanks.</description><pubDate>Tue, 01 Jun 2010 08:20:08 GMT</pubDate><dc:creator>Tom Garth</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>A very good question.  I had to take a guess at whether SQL Server would continue the downward sequence correctly after the IDENTITY_INSERT operation.  The correct behaviour is logical, but there have been so many bugs with IDENTITY, it's hard to keep track.My current favourite example:[url]https://connect.microsoft.com/SQLServer/feedback/details/492452/generate-identity-value-is-incorrect-after-set-identity-insert-on-on-an-empty-table[/url]</description><pubDate>Tue, 01 Jun 2010 01:13:52 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Three answers:1) I do not think that the documentation covers this topic very well, even though it is very intuitive. That is why I posted the question here.2) Yes, -8 would have sufficed, but I set it to such a crazy large number to demonstrate a point. I always gets points better when they have crazy large numbers in them. :)3) Yes, the temporary tables end when the session ends. I dropped them because of best practices/force of habit. It's like I tell my children, you have to put away your toys when you are done playing with them.</description><pubDate>Mon, 31 May 2010 14:06:43 GMT</pubDate><dc:creator>ron.carpenter</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Three questions:1) I believe the outcome is the most logical, but why doesn't the documentation make mention of the fact the identity will march on irregardless of any identity_insert?http://msdn.microsoft.com/en-us/library/ms186775%28SQL.90%29.aspxhttp://msdn.microsoft.com/en-us/library/ms188059.aspx2) For #Temp1 why was the inital value set to -2147483648, such a crazy large (small) number?  Wouldn't -8 have sufficed?3) Do the temporary tables really need to be dropped?  Don't they just disappear when the session ends?Thanks for the learning opportunity!</description><pubDate>Mon, 31 May 2010 10:12:42 GMT</pubDate><dc:creator>SQL Server Youngling</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Nice question.</description><pubDate>Mon, 31 May 2010 08:06:12 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>If you want an increment of 10, so all values are 0, 10, 20 and so on,be warn of SET IDENTITY_INSERT &amp;lt;table&amp;gt; ONmay lead to unwanted sequence:create table #a(i int not null identity(0,10),v varchar(1))SET IDENTITY_INSERT #a ONINSERT INTO #a (	 i	,v )SELECT 	 7	,'A'SET IDENTITY_INSERT #a OFFINSERT INTO #a (	 v )SELECT 	 'B'SELECT * FROM #aResult:i	v---      ----7	A17	B(2 row(s) affected)A check constraint should be used to avoid bad values for identity.</description><pubDate>Mon, 31 May 2010 01:18:55 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Had to stretch the grey matter on this one.good question.</description><pubDate>Mon, 31 May 2010 01:00:26 GMT</pubDate><dc:creator>Stewart "Arturius" Campbell</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Had to think this one through, and managed to get it right!  Good question!</description><pubDate>Mon, 31 May 2010 00:09:59 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>good one... thanks :-)</description><pubDate>Mon, 31 May 2010 00:06:30 GMT</pubDate><dc:creator>ziangij</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Good question.A better (more explicit) MSDN reference would be:http://msdn.microsoft.com/en-us/library/ms188059(v=SQL.90).aspxThe following extract from the 'Remarks' section covers the issue:'If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value.'(Where 'larger' is relative to the direction of the identity.)This extract is better than that provided on the question's Answer's Explanation because it clearly indicates it is the last insert itself that results in the identity value being updated (i.e. not something that is evaluated before the next insert).One can override the setting via IDENT_SEED and then all kinds of problems can be generated (duplicated identity values, etc.).</description><pubDate>Sun, 30 May 2010 21:58:44 GMT</pubDate><dc:creator>murray-906152</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>This is a great question, thank you Ron!Hats off to the database engine team on this one, the implementation of the seeding next value in the direction of open waters after the identity insert is set back to off is absolutely perfect. This completely prevents the possibility of run-offs.Oleg</description><pubDate>Sun, 30 May 2010 19:23:18 GMT</pubDate><dc:creator>Oleg Netchaev</dc:creator></item><item><title>RE: What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>It was little tough...</description><pubDate>Sat, 29 May 2010 23:37:46 GMT</pubDate><dc:creator>SQL-DBA-01</dc:creator></item><item><title>What identity comes next?</title><link>http://www.sqlservercentral.com/Forums/Topic930099-2659-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/IDENTITY/69891/"&gt;What identity comes next?&lt;/A&gt;[/B]</description><pubDate>Sat, 29 May 2010 21:05:46 GMT</pubDate><dc:creator>ron.carpenter</dc:creator></item></channel></rss>