July 15, 2009 at 2:42 pm
Hi guys,
I want to create a script to loop in all tables to reseed the identities in a specific database
using:
DBCC CHECKIDENT(‘TableName’, RESEED, current_table_ident + 1)
How can i make a loop for this ?!!!
Thanks in advance!
July 15, 2009 at 5:46 pm
Here's one method. Note the command is undocumented. Look around this site if you need more info on it. The NORESEED option is shown here to prevent someone from doing damage with a quick copy/paste/run.
Exec sp_MSForEachTable 'DBCC CHECKIDENT("?", NORESEED) '
July 15, 2009 at 9:39 pm
ahmad.ghazi (7/15/2009)
Hi guys,I want to create a script to loop in all tables to reseed the identities in a specific database
using:
DBCC CHECKIDENT(‘TableName’, RESEED, current_table_ident + 1)
How can i make a loop for this ?!!!
Thanks in advance!
Why would you want to do this? What would be the business reason behind doing such a thing?
--Jeff Moden
Change is inevitable... Change for the better is not.
July 16, 2009 at 2:45 am
ok Jeff what i have is a peer to peer transactional replication on sql server 2005
and i need to adjust identity ranges for this because i have set the identity columns in the publisher to seed with 1 and increment by 2 then when i restore this database in the subscriber i want to change the seed to be 2 then i have to reseed the identity column in the publisher using the following command:
DBCC CHECKIDENT
(TableName,'reseed',HighestValueFoundInSubscriber+1)
and i have over 200 table and it's hard to make it manually
any other suggestion
Thanks
July 16, 2009 at 6:44 am
ahmad.ghazi (7/16/2009)
ok Jeff what i have is a peer to peer transactional replication on sql server 2005and i need to adjust identity ranges for this because i have set the identity columns in the publisher to seed with 1 and increment by 2 then when i restore this database in the subscriber i want to change the seed to be 2 then i have to reseed the identity column in the publisher using the following command:
DBCC CHECKIDENT
(TableName,'reseed',HighestValueFoundInSubscriber+1)
and i have over 200 table and it's hard to make it manually
any other suggestion
Thanks
Thanks for taking the time to explain, Ahmad. I appreciate it. I'll have to sit down and figure out why you might have needed to go through all those gyrations with the increment by 2 because I don't currently understand why that was needed just to support replication.
In any case, the use of sp_MSForEachTable like KSullivan suggested would probably do the trick. If you need to include only replicated tables, you'll need to read which tables are being replicated from the system views (I think that can still be done in 2k5) or maintain a separate list and then step through that list. This is one of the few places where a loop could actually be condoned.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply