0

Possible Duplicate:
How do I add auto_increment to a column in SQL Server 2008

Hey all i created the table in gui however i forgot to set the primary key to autoincrement! name of the table is Emp_CV.

Something like this:

alter table Emp_CV alter column Applicant_ID NOT NULL int AUTO_INCREMENT

how can i accomplish this? Am using SQL Server 2008!

Community
  • 1
  • 1
Plotter
  • 39
  • 1
  • 3
  • 12

1 Answers1

1

The short answer is...you can't. Here's what you have to do:

  1. Ensure nobody/nothing is using your table.

  2. Add a new integer column that will be the new identity column. It must allow nulls and must have the appropriate identity value.

  3. execute set identity_insert dbo.foo on -- must specify your table name.

  4. Seed it with the values of the existing identity column:

    update dbo.foo set new_id = id

  5. Drop any keys/constraints in which the existing identity column participates.

  6. execute set identity_insert dbo.foo off

  7. Drop the existing column.

  8. Alter the new column, changing its nullity to not null.

  9. Execute the command dbcc checkident( {your-table-name-here} , reseed ).

  10. Execute sp_rename and give the new column the same name as the old column.

  11. Recreate the keys/constraints dropped in step #4.

  12. Might want to run sp_recompile on any stored procedures referencing this table.

Easy!

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135