Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Incrementing a field Expand / Collapse
Author
Message
Posted Thursday, September 30, 2010 2:46 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 6, 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?
Post #995784
Posted Thursday, September 30, 2010 3:40 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
There is something called "ROW_NUMBER" .. Please refer Books Online (free help that comes with SQL Server) to learn about Row_NUMBER...
Post #995799
Posted Thursday, September 30, 2010 4:04 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:24 AM
Points: 12,887, Visits: 31,835
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
Post #995809
Posted Thursday, September 30, 2010 4:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 6, 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


Post #995815
Posted Thursday, September 30, 2010 4:30 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:24 AM
Points: 12,887, Visits: 31,835
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
Post #995819
Posted Thursday, September 30, 2010 4:52 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 6, 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



Post #995821
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse