Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 May 23rd, 2005, 02:00 PM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to auto increment primary key

i wonder how i can autom increment primary key of table when ever i insert a new record using asp.net web form? i want to insert some data to sql server db using web forms.Thank u and looking forward to your replyes.Thanks
 
Old May 23rd, 2005, 02:13 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you are familiar with Access Autonumber, you can achieve similar functionality by using IDENTITY column in SQL server.
Create an ID column(field) in your db, set it as an identity column.

Search for "Identity" in SQL server books online

 
Old May 23rd, 2005, 02:22 PM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by shahchi1
 If you are familiar with Access Autonumber, you can achieve similar functionality by using IDENTITY column in SQL server.
Create an ID column(field) in your db, set it as an identity column.

Search for "Identity" in SQL server books online

Thank u for u reply. do u mean i need to change the
data type of primary key to some thing diffrent in sql server? Can it be done inside the asp.net web form ?
 
Old May 23rd, 2005, 03:09 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, maintaining integrity of data is a database issue and must be done in your database. if you are using SQL server, in the enterprise manager set primary key field data type to integer and set column property to identity.

If all of the above does not make sense, my humble request get a SQL server book and read..
 
Old May 24th, 2005, 12:30 PM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by shahchi1
 No, maintaining integrity of data is a database issue and must be done in your database. if you are using SQL server, in the enterprise manager set primary key field data type to integer and set column property to identity.

If all of the above does not make sense, my humble request get a SQL server book and read..


Many thanks to your reply. Well i did that but now when i insert records it start incrementing from 113 not from 0/one !! Before the changes i already had some records do u think that is the problem or primary key datatype ?Thanks and looking forward to your reply.
 
Old May 24th, 2005, 03:25 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No problem you can use DBCC CHECKIDENT to reseed the identity value to start from 1

This example forces the current identity value in the jobs table to a value of 30.

USE pubs
GO
DBCC CHECKIDENT (jobs, RESEED, 30)
GO
Also,
The current identity value can be larger than the maximum value in the table. DBCC CHECKIDENT does not reset the current identity value automatically in this case. To reset the current identity value when it is larger than the maximum value in the column, use either of two methods:

Execute DBCC CHECKIDENT ('table_name', NORESEED) to determine the current maximum value in the column, and then specify that as the new_reseed_value in a DBCC CHECKIDENT ('table_name', RESEED, new_reseed_value) statement.


Execute DBCC CHECKIDENT ('table_name', RESEED, new_reseed_value) with new_reseed_value set to a very low value, and then run DBCC CHECKIDENT ('table_name', RESEED).





Similar Threads
Thread Thread Starter Forum Replies Last Post
auto increment primary key jcuga SQL Language 2 June 3rd, 2007 09:04 AM
Increment a primary key bvpsekhar MySQL 7 May 9th, 2007 07:09 AM
SQL query to find key when auto-increment SandyFeder SQL Server ASP 1 November 21st, 2005 10:49 AM
auto gerate of primary key Abhinav_jain_mca SQL Server 2000 15 October 1st, 2004 05:51 AM
auto increment primary key-compile error junemo Oracle 2 June 16th, 2004 08:53 AM





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