CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    How to add an identity to an existing column?

    I wander if it is possible to add an identity to an existing column in SQL server? If yes, could you please give an example. Thanks for your inputs.

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    50

    Re: How to add an identity to an existing column?

    I would suggest you create a new empty table with the same structure as your existing table except the column you wish to change is now of type IDENTITY and a different table name.

    Then INSERT all records from your old table to the new table but first SET IDENTITY_INSERT as decribed in the following link. Make sure you provide a unique value for your new identity column.

    http://www.sqlteam.com/article/how-t...-in-sql-server

    Then turn off IDENTITY_INSERT.

    Finally drop the original table (or rename it out of the way as a back up). Then rename the new table to have the name of the old table. Look at the system procedure "sp_rename" for renaming tables.
    sp_rename 'old_table_name', new_table_name'

    I hope that helps.

    Cheers,

    Kevin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured