all groups > sql server msde > july 2007 >
You're in the

sql server msde

group:

Resetting the count on a Primary Key at a certain record? Impossible?



Resetting the count on a Primary Key at a certain record? Impossible? Matt Brown - identify
7/2/2007 10:51:23 AM
sql server msde: Hello,

Several consecutive records have been deleted out of an MSDE database
and the program that uses the database doesn't like it. I'm curious
if it's possible to... say... reset the primary key count so that the
records all shift to fill the "dead space" left by the deleted
records.

I have the feeling that this may be humorous to some of you, but any
input (including "you can't do this. that's the point of a primary
key") is appreciated.


Thanks!

Matt
Re: Resetting the count on a Primary Key at a certain record? Impossible? Andrea Montanari
7/2/2007 10:37:15 PM
hi Matt,
[quoted text, click to view]

you can "reset" the identity value using a DBCC CHECKIDENT with the RESEED
option specified..
http://msdn2.microsoft.com/en-us/library/ms176057.aspx
--
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz http://italy.mvps.org
DbaMgr2k ver 0.21.0 - DbaMgr ver 0.65.0 and further SQL Tools
--------- remove DMO to reply

Re: Resetting the count on a Primary Key at a certain record? Impossible? Andrea Montanari
7/3/2007 12:00:00 AM
[quoted text, click to view]

ooopppsss...
Hugo is right, completely right... I did not read with enougth attention and
only saw the "identity" resetting requirement... I'm sorry..
I'm just curious why "..program that uses the database doesn't like it.." :D
Agreeing completely with Hugo about the "keys" immutability, you can perhaps
workaround that..
you could create a "temporary" table where you insert all the current valid
rows of your table, truncate the original table in order to empty it and to
reset the idientity table's value, and finally refill it with the "orphaned"
rows..
so you can write something like
INSERT INTO tempTable SELECT * FROM dbo.YourTable WHERE... ORDER BY...;

TRUNCATE TABLE dbo.YourTable;

INSERT INTO dbo.YourTable (all_columns_but_the_identity_col)
SELECT all_columns_but_the_identity_col
FROM tempTable
WHERE... ORDER BY...;

DROP TABLE tempTable;

but, again, this is a "poor strategy"... you'll end up with gaps anyway, now
or then.. and this "workaround" does not work if you have declarative
referential integrity set on that table, if it's referenced by other rows in
other tables...
--
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz http://italy.mvps.org
DbaMgr2k ver 0.21.0 - DbaMgr ver 0.65.0 and further SQL Tools
--------- remove DMO to reply

Re: Resetting the count on a Primary Key at a certain record? Impossible? Hugo Kornelis
7/3/2007 12:27:47 AM
[quoted text, click to view]

Hi Matt,

You can't do this. That's the point of a primary key.

Seriously - a primary key is intended to identify an entity. Think of
what would happen if you would change your name. Or get a new SSN.

Note that I am not contradicting Andrea. DBCC CHECKIDENT WITH RESEED
will affect new IDENTITY values, not existing values. I think that you
were asking about the latter.

--
Hugo Kornelis, SQL Server MVP
Re: Resetting the count on a Primary Key at a certain record? Impossible? Matt Brown - identify
7/10/2007 7:50:02 PM
On Jul 3, 6:34 am, "Andrea Montanari" <andrea.sql...@virgilio.it>
[quoted text, click to view]

I haven't checked this is a while. That's actually perfect. I can
remove some recordsets, then reset the identity key count with this
here DBCC CHECKIDENT WITH RESEED, then everything should progress as
normal.

Righto!

Thanks,

Matt
AddThis Social Bookmark Button