Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Incrementing a field


Incrementing a field

Author
Message
sc-w
sc-w
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 393
Hi,
I have a table with thousands of rows of data and i really need to add a new field called "autonumber" and in that field i need to update each row with an incrementing number starting from one. It does not matter what record starts with the numbering but i need them to all get an auto number.

I've tried things i've found online but not getting anywhere, any advice please?
ColdCoffee
ColdCoffee
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2319 Visits: 5545
There is something called "ROW_NUMBER" .. Please refer Books Online (free help that comes with SQL Server) to learn about Row_NUMBER...
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14984 Visits: 39013
another option is to use the SSMS GUI and design the table;
if you add the new column, and make sure you select that it has the identity property, the GUI will rebuild the table and populate the column with default values, as well as guaranteeing that new rows get the new value.



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

sc-w
sc-w
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 393
Thank you for your help but the answer that i needed was this so i help it helps someone else.


alter table yourtable
add autonumber int identity(1,1)
go



Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14984 Visits: 39013
sc-w (9/30/2010)
Thank you for your help but the answer that i needed was this so i help it helps someone else.


alter table yourtable
add autonumber int identity(1,1)
go




yes that works, but it adds the column to the end of the table.
I've always tried to design tables so the identity is the first column in the table.
also, the column name of that identity is the same as the table itself, plus a suffix, ie the table City has the first column as CityID int identity(1,1) for example.

i find it makes it much easier and more intuitive when you have to write queries that are joining together, where if you know the table name, you can infer the identity by tablename + TBLKEY or ID or whatever is comfortable for you.

in your example, would every table have an "autonumber" column?

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

sc-w
sc-w
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 393
No, just this table, it's not to make it relational, my requirements are simple I have a table that is relational to other tables but based on an already existing uniqueid field.

I happen to now need a field that auto increments which is simple to setup up if you have no data in a table, had I know my current requirements when the system was built I would have done this.

However, the table now has 1,000,000 rows in it and I can't just set a field to auto increment without first updating the existing rows with a number so the code I referenced worked perfectly for me. I’m sure there is other people out there that could benefit.

Thanks




alter table yourtable
add autonumber int identity(1,1)
go




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