SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Identity puzzle


Identity puzzle

Author
Message
raymond lew
raymond lew
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 123
Comments posted to this topic are about the item Identity puzzle
Carlo Romagnano
Carlo Romagnano
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4939 Visits: 3326
Thanks! Some times sql behave in unexpected manner.

I run on tuttopodismo
Rachel Byford
Rachel Byford
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1684 Visits: 963
Nice question
Steve S.-542474
Steve S.-542474
SSC-Enthusiastic
SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)

Group: General Forum Members
Points: 127 Visits: 70
Okay, I'm lost. If RESEED sets the value back to zero and this table was RESEEDed right before both queries, then why wouldn't both queries return zero?
Chad Crawford
 Chad Crawford
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2838 Visits: 18717
The result depends on whether or not any rows have been inserted into the table since it was created. If there haven't (like in the first reseed), the next value inserted is the reseed value. If there have, then the next value inserted is the reseed + 1. Interestingly enough, this ignores the “increment” value of the identity column. If you TRUNCATE the table instead of deleting the rows, the table acts as if there have been no rows added and the reseed uses the reseed value again. It is odd behavior, but it is also documented in BOL.

Thanks,
Chad
noeld
noeld
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9616 Visits: 2048
Am I looking at the same question you are ?
There is no TRUNCATE TABLE anywhere in the code!
Sad


* Noel
noeld
noeld
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9616 Visits: 2048
Nevermind, I think I am in need of coffee Wink


* Noel
Steve S.-542474
Steve S.-542474
SSC-Enthusiastic
SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)

Group: General Forum Members
Points: 127 Visits: 70
Chad - got it. Thanks!
raymond lew
raymond lew
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 123
I am glad to see so many people getting this right. And for 3 points!

It certainly does not operate according to how I expect things to work. I just wished I had posted something more challenging than a simple script.Wink

Perhaps something like:

Given a table defined as

CREATE TABLE id_test (my_id INT IDENTITY(1,1) NOT NULL)

and

(select count(*) from given_table) = 0

Select the right action to take to ensure that the next row inserted is 1

a) DBCC CHECKIDENT(id_test, RESEED, 0)
b) DBCC CHECKIDENT(id_test, RESEED, 1)
c) it depends, a) OR b) depending on the circumstance.

Bonus question:
If you select c, you are correct. Now how would you determine which reseed statement to perform?


I am really interested in the answer to the bonus question as I have not been able to crack this.
Tim Wilson-Brown
Tim Wilson-Brown
SSC Veteran
SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)

Group: General Forum Members
Points: 277 Visits: 302
raymond lew (3/3/2009)
I am glad to see so many people getting this right. And for 3 points!

It certainly does not operate according to how I expect things to work. I just wished I had posted something more challenging than a simple script.Wink

Perhaps something like:

Given a table defined as

CREATE TABLE id_test (my_id INT IDENTITY(1,1) NOT NULL)

and

(select count(*) from given_table) = 0

Select the right action to take to ensure that the next row inserted is 1

a) DBCC CHECKIDENT(id_test, RESEED, 0)
b) DBCC CHECKIDENT(id_test, RESEED, 1)
c) it depends, a) OR b) depending on the circumstance.

Bonus question:
If you select c, you are correct. Now how would you determine which reseed statement to perform?


I am really interested in the answer to the bonus question as I have not been able to crack this.



The answer to the bonus question is to use DBCC CHECKIDENT without reseeding Smile


CREATE TABLE id_test ( my_id INT IDENTITY(1,1) NOT NULL)

DBCC CHECKIDENT(id_test)

INSERT INTO id_test DEFAULT VALUES;
DELETE FROM id_test

DBCC CHECKIDENT(id_test)

INSERT INTO id_test DEFAULT VALUES;
DELETE FROM id_test

DROP TABLE id_test
go



Returns


Checking identity information: current identity value 'NULL', current column value 'NULL'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

(1 row(s) affected)

(1 row(s) affected)
Checking identity information: current identity value '1', current column value '1'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

(1 row(s) affected)

(1 row(s) affected)



If DBCC CHECKIDENT (NORESEED) reports NULL for the current identity value, use 1.
Otherwise, if it reports 1, use 0.


CREATE TABLE id_test ( my_id INT IDENTITY(1,1) NOT NULL)

DBCC CHECKIDENT(id_test)

DBCC CHECKIDENT (id_test, RESEED, 1)--reseed to 1

INSERT INTO id_test DEFAULT VALUES;
select my_id FROM id_test --select number 1
DELETE FROM id_test

DBCC CHECKIDENT(id_test)

DBCC CHECKIDENT (id_test, RESEED, 0)--reseed to 0 + 1

INSERT INTO id_test DEFAULT VALUES;
select my_id FROM id_test --select number 2
DELETE FROM id_test

DROP TABLE id_test
go



I'll leave you with the challenge of parsing the output from DBCC CHECKIDENT.

Of course, there is also a trivial answer:


TRUNCATE TABLE id_test -- reset table to a known state
DBCC CHECKIDENT (id_test, RESEED, 1)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search