Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2009, 04:58 PM
Registered User
 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default reset an auto-increment field each month

hi all,

i have an auto-increment field and need to reset it each month so it will not get a big value
example:
in the end of february the id was 34576 on the start of march i need to reset this field automatically to start with 1 any idea how to do it?
 
Old April 2nd, 2009, 06:24 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Yes, but tell me, what do you want to do with the numbers in that column that already exist? You cannot simply reset the numbers to 1 if 1 already exists in that column? Now, if this is just for display purposes, there's better ways to do it and it won't break any Declarative Referential Integrity (DRI) that you may have.
__________________
--Jeff Moden
 
Old April 2nd, 2009, 06:47 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

If the column does not have a unique constraint and is not a primary key, this is possible, but as Jeff is getting at, it is not necessarily the best method to use.

There are many ways to do this, or achieve the same effect, but which way you choose depends on:
- will you keep existing data from previous months?
- similarly, is this a primary key column?
- what type of front-end or processes are you using to manipulate data (web site, automated services)? It may be more appropriate to work out the value to use on the front end.

For your information tho, you can reset an identity seed by truncating the table (TRUNCATE TABLE MyTable;) or by using a DBCC RESEED command:
sql Code:
-- reseed to 0, so next entry gets an id of 1
DBCC CHECKIDENT ('MyTable', RESEED, 0);
 
Old April 3rd, 2009, 02:41 AM
Registered User
 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks guys for the quick reply,
i need to keep the old data, i am using a purchased software. i added a field in the database and i need this field in a report
it is not a problem to remove the primary key
 
Old April 3rd, 2009, 09:31 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Actually, it IS a problem if you remove the primary key for many reasons. Read about what a "heap table" is and what some of the disadvantages are including the possible reduction in performance.

What would probably help us help you, at this point, is the CREATE statement for the table and any indexes and triggers that may also be on the table. Provided there isn't any private or sensitive information in them, a sampling of rows contained in the table would help, as well.
__________________
--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto Increment Field Problem in SQL Server 2000 yes_no ASP.NET 1.0 and 1.1 Professional 4 August 11th, 2008 07:10 AM
Auto increment prad_a MySQL 3 April 7th, 2007 05:47 AM
How to auto increment ? Shawn Mohan SQL Server 2000 2 June 22nd, 2006 03:00 AM
How to auto increment? Shawn Mohan ASP.NET 2.0 Basics 6 June 20th, 2006 10:36 PM
auto increment? hosefo81 PHP Databases 1 February 2nd, 2004 03:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.