October 22, 2012 at 2:12 am
Koen Verbeeck (10/22/2012)
Lokesh Vij (10/22/2012)
kapil_kk (10/22/2012)
in this case the max ident value would be 101 as with first insert statement the value of ident was 100 and with another insert statement the ident value would be 101 and max from 101 and 100 is 101 so 101 is the max value..hope its clear to you
Actually the question posted by John is little different. Execute the below code:
CREATE TABLE IdentTest
( Ident INT NOT NULL IDENTITY (1,1)
, varfield varchar(100)
)
INSERT INTO IdentTest VALUES ('xyz') -- <== Added to original
INSERT INTO IdentTest VALUES ('123') -- <== Added to original
DBCC CHECKIDENT ('IdentTest',RESEED,100)
INSERT INTO IdentTest VALUES ('abc')
SELECT Ident FROM IdentTest -- <== (done away with max)
Now the last select statement (done away with max statement intentionally), returns three values 1,2 and 101
Why value 101?? Why not 100 or 102??
If data is present in the table before DBCC CHECKIDENT, the new identity value is the reseed value + current incremenet. In this case, 100 + 1 = 101.
Anyway, nice question.
Thank you Koen for the good succinct way to state how this works. It did tickle my urge to keep playing, though...
Try predicting the result of the final SELECT in this script:
CREATE TABLE IdentTest
( --Ident INT NOT NULL IDENTITY (1,1)
varfield varchar(100)
)
INSERT INTO IdentTest VALUES ('xyz')
INSERT INTO IdentTest VALUES ('123')
Select * from IdentTest
ALTER TABLE IdentTest
Add Ident INT IDENTITY (1,1)
INSERT INTO IdentTest VALUES ('abc')
SELECT Ident FROM IdentTest
where varfield = 'abc'
October 22, 2012 at 2:27 am
This was removed by the editor as SPAM
October 22, 2012 at 4:52 am
Nice easy question.
Tom
October 22, 2012 at 5:10 am
Nice one, thanks
Iulian
October 22, 2012 at 5:43 am
Good question, Thanks. Also, enjoy the discussion that followed.
October 22, 2012 at 9:57 am
Stuart Davies (10/22/2012)
Good question and thanks for an easy start to the week.
Agreed! +1
Not all gray hairs are Dinosaurs!
October 22, 2012 at 12:50 pm
Interesting Question and good topic. +1
October 22, 2012 at 1:11 pm
Nice tricky question.
No rows inserted, therefore the answer is 100.
October 22, 2012 at 1:15 pm
john.arnott (10/21/2012)
Good topic for a follow-up QOD (hope I don't steal any thunder here):If the code presented had one more statement amnd asked for the max(Ident)...
CREATE TABLE IdentTest
( Ident INT NOT NULL IDENTITY (1,1)
, varfield varchar(100)
)
INSERT INTO IdentTest VALUES ('xyz') -- <== Added to original
DBCC CHECKIDENT ('IdentTest',RESEED,100)
INSERT INTO IdentTest VALUES ('abc')
SELECT max(Ident) FROM IdentTest
What would be the output?
Wouldn't it be the same (100)?
If not, WHY NOT? (hard to make a QOD an essay question, but if you got this far, give it a shot).
Does anyone think I should go ahead and add this as a follow up QOTD? Or do one about reseeding causing a duplicate identity? Or not bother since it was discussed here already?
Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]
October 22, 2012 at 1:26 pm
crussell-931424 (10/22/2012)
The effective result is different for a table that has had records from a table that has never had any records. Don't know why. It just is.
Not quite - it depends how you deleted the records. If you deleted them using DELETE, it's as you describe (the first record after reseed gets the incremented number) but if you deleted them using TRUNCATE TABLE it's as if there had never been any records (the first record after reseed gets the specified number, unincremented).
Tom
October 22, 2012 at 3:26 pm
L' Eomot Inversé (10/22/2012)
crussell-931424 (10/22/2012)
The effective result is different for a table that has had records from a table that has never had any records. Don't know why. It just is.Not quite - it depends how you deleted the records. If you deleted them using DELETE, it's as you describe (the first record after reseed gets the incremented number) but if you deleted them using TRUNCATE TABLE it's as if there had never been any records (the first record after reseed gets the specified number, unincremented).
If I had to guess an identity column actually has 3 properties .. Seed, Increment, and HaveIHadRowsBefore or something along those lines.
If the 3rd property is true then the next row is Seed+1, if the 3rd property is false then the next row is just Seed.
The DBCC just changes the Seed property. Truncate resets all of the properties back to the original table definition. Which I guess would actually require 2 additional properties to hold the original settings.
Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]
October 22, 2012 at 10:06 pm
Koen Verbeeck (10/22/2012)
Lokesh Vij (10/22/2012)
kapil_kk (10/22/2012)
in this case the max ident value would be 101 as with first insert statement the value of ident was 100 and with another insert statement the ident value would be 101 and max from 101 and 100 is 101 so 101 is the max value..hope its clear to you
Actually the question posted by John is little different. Execute the below code:
CREATE TABLE IdentTest
( Ident INT NOT NULL IDENTITY (1,1)
, varfield varchar(100)
)
INSERT INTO IdentTest VALUES ('xyz') -- <== Added to original
INSERT INTO IdentTest VALUES ('123') -- <== Added to original
DBCC CHECKIDENT ('IdentTest',RESEED,100)
INSERT INTO IdentTest VALUES ('abc')
SELECT Ident FROM IdentTest -- <== (done away with max)
Now the last select statement (done away with max statement intentionally), returns three values 1,2 and 101
Why value 101?? Why not 100 or 102??
If data is present in the table before DBCC CHECKIDENT, the new identity value is the reseed value + current incremenet. In this case, 100 + 1 = 101.
Anyway, nice question.
agreed, nice question and nice discussion.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
October 25, 2012 at 7:05 am
john.arnott (10/22/2012)
Try predicting the result of the final SELECT in this script:
CREATE TABLE IdentTest
( --Ident INT NOT NULL IDENTITY (1,1)
varfield varchar(100)
)
INSERT INTO IdentTest VALUES ('xyz')
INSERT INTO IdentTest VALUES ('123')
Select * from IdentTest
ALTER TABLE IdentTest
Add Ident INT IDENTITY (1,1)
INSERT INTO IdentTest VALUES ('abc')
SELECT Ident FROM IdentTest
where varfield = 'abc'
Make this more fun, make varfield a primary key.
October 25, 2012 at 7:08 am
Kenneth.Fisher (10/22/2012)
Does anyone think I should go ahead and add this as a follow up QOTD? Or do one about reseeding causing a duplicate identity? Or not bother since it was discussed here already?
It looks like there are a few possible follow up questions. Since not everyone reads the discussion I think it's worth submitting additional ones.
October 25, 2012 at 9:06 am
cfradenburg (10/25/2012)
Kenneth.Fisher (10/22/2012)
Does anyone think I should go ahead and add this as a follow up QOTD? Or do one about reseeding causing a duplicate identity? Or not bother since it was discussed here already?It looks like there are a few possible follow up questions. Since not everyone reads the discussion I think it's worth submitting additional ones.
+1
It also makes sense to post a QOTD because there is a lead time of about 2-3 weeks if the question is posted today 🙂
~ Lokesh Vij
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
Viewing 15 posts - 16 through 29 (of 29 total)
You must be logged in to reply to this topic. Login to reply