|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, December 06, 2011 11:09 AM
Points: 148,
Visits: 331
|
|
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?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 4:37 PM
Points: 2,248,
Visits: 5,352
|
|
| There is something called "ROW_NUMBER" .. Please refer Books Online (free help that comes with SQL Server) to learn about Row_NUMBER...
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 11,609,
Visits: 27,658
|
|
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
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, December 06, 2011 11:09 AM
Points: 148,
Visits: 331
|
|
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
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 11,609,
Visits: 27,658
|
|
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
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, December 06, 2011 11:09 AM
Points: 148,
Visits: 331
|
|
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
|
|
|
|