|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, April 24, 2012 3:11 AM
Points: 1,023,
Visits: 174
|
|
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.
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 10:33 AM
Points: 10,989,
Visits: 10,529
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, February 04, 2011 7:20 AM
Points: 977,
Visits: 1,499
|
|
Tricky. I had a 50-50 chance and blew it!
Good question. Thanks.
Tom Garth Vertical Solutions
"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 3:05 PM
Points: 2,117,
Visits: 2,209
|
|
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 >= 1, MIN(ID) - 1, if the increment is <= -1.
Here is the sample code with two additional queries I wrote to help me understand it.
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,Temp2ID FROM #Temp1 t1 INNER JOIN #Temp2 t2 ON t1.Temp1Value = t2.Temp2Value WHERE t1.Temp1Value = '4' ;
DROP TABLE #Temp1 ; DROP TABLE #Temp2 ;
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. Otherwise, as others have noted, you would end up with ID collisions when using negative increments.
Thanks, webrunner
------------------- "The chemistry must be respected." - Walter White
"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'" Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: 2 days ago @ 1:46 PM
Points: 18,732,
Visits: 12,329
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
| Another great question, I had to think about this one, but it makes sense. Thanks!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 11:53 AM
Points: 2,672,
Visits: 2,416
|
|
| I had to puzzle on this one for a while. Thanks.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, April 12, 2013 3:17 PM
Points: 30,
Visits: 39
|
|
So, can we say that what SQL Server does to determine the next identity value is:
Select Sign(Ident_Incr('tablename')) * Max(Sign(Ident_Incr('tablename')) * $IDENTITY) + Ident_Incr('tablename') From tablename
where $IDENTITY is the identity column of table tablename ?
|
|
|
|