Total Pageviews

Showing posts with label Today's Question. Show all posts
Showing posts with label Today's Question. Show all posts

Tuesday, May 10, 2011

Today's Question!!!

This question is taken from http://www.sqlservercentral.com/questions/T-SQL/73459/. A very interesting question:



CREATE TABLE MyIdentityTest( id INT IDENTITY(1,1)
, mychar VARCHAR(10)
)
GOINSERT MyIdentityTest SELECT 'A'
GOSET IDENTITY_INSERT MyIdentityTest ON
INSERT MyIdentityTest (id, mychar) SELECT 12, 'L'
GOSET IDENTITY_INSERT MyIdentityTest OFF
GODBCC CHECKIDENT(myidentitytest, RESEED, 2)
GOINSERT MyIdentityTest SELECT 'B'
SELECT id 
 FROM myidentitytest 
 WHERE mychar = 'B'
DROP TABLE myidentitytest

Answer:3
Reason:When you reseed the identity value, you set the counter to the value you specify. In this case, the value is set to "2", and the next insert takes a 3.