Incrementing a field

  • 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?

  • There is something called "ROW_NUMBER" .. Please refer Books Online (free help that comes with SQL Server) to learn about Row_NUMBER...

  • 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!

  • 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

  • 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!

  • 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

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply