views:

739

answers:

4

I wanted to modify a column in a sql server 2005 table to IDENTITY(1,1)
Incidentally this table is empty and the column to be changed is a primary key.
This column is also a foreign key for two other tables.

After googling I found that you cannot use Alter table syntax to modify a column and make it an indentity column.
Link #1 : How do I add the identity property to an existing column in SQL Server
Link #2 : Adding an identity to an existing column -SQL Server

I ended up checking the dependent tables (2 of them) removing the foreign keys (generated the script from SSMS) then dropping the main table then re-creating with identity. (could try the rename option here as well)
Then re-created the foreign keys for the earlier dependent two tables.

But all this was manual work, any scripts or SPs out there to make this easier.

Ideally all these steps would be done by such a script/tool/utility:

  1. Check dependent tables keys
  2. Generate Create and drop foreign key scripts for this
  3. Generate create script for the main table
  4. drop the main table (or rename the table if the table has data)
  5. re-create the table with identity column enabled
  6. re-create foreign keys
+2  A: 

You can use SSMS to generate a script (Edit a table, save script), but otherwise it's a manual process as you identified.

The SSMS scripts will pick up dependencies etc. For this kind of work, I tend to use SSMS to generate a basic script, pimp it a bit, run it carefully, then use a comparison tool (such as Red Gate compare) to generate a safer version.

Edit: The SSMS error is not an error, it's a safety check that can be switched off

gbn
+1, there are so many things to change with tables/columns/keys/constraints/etc. it would be impossible to have a utility for each. that's why you can use the GUI to generate a script
KM
SSMS throws an error, this was the first option I tried. Should have mentioned in the Question...
Binoj Antony
Thanks for the link, disabling the option worked like a charm.
Binoj Antony
+1  A: 

(This is merely a follow-up to gbn's post with more details -- it isn't all that easy to figure this stuff out.)(

It isn't impossible to write a utility to do this, just very complex and very hard. Fortunately, Microsoft has already done it -- its called SSMS (or SMO?). To generate such a script:

  • In the Object Explorer, drill down to the database and table that you want to modify Right click and select Design

  • Make the desired changes to the one table in the design screen. It's reasonably intuitive.

  • To add/remove the identity property, select the column in the upper pane, and in the lower pane/"Column Properties" tab, expand and configure the settings under "Identity Specification".

  • To generate a script to implement all your changes, incorporating all the dependent key changes, click on the "Generate Change Script" toolbar button. This is also an option under the "Table Designer" menu.

I also do this to generate scripts (that I later modify--SSMS doesn't always produce the most efficient code.) Once done, you can exit out without saving your changes -- leaving you a DB you can test your new script on.

Philip Kelley
Thanks, I didn't know if I should go details...
gbn
I only stumbled across this feature by accident long ago. Early on, generating and reviewing these scripts taught me a lot about SQL; later on it led me to many ways how not to write SQL. (I'd guess the code generated by SSMS is--has to be--compatible with SQL back through 7.0, and perhaps earlier?)
Philip Kelley
Already tried this, in fact this was the first thing I tried. I gives an error "Saving Changes not permitted, The changes you have made require the following table to be dropped.......require the table to be re-created"
Binoj Antony
@Binoj Antony: It's a check, not an error...http://www.sql-server-performance.com/faq/saving_changes_not_permitted_p1.aspx
gbn
Thanks Philip, your answer was helpful since both the answer is correct I need to select one :(
Binoj Antony
A: 

What's wrong with:

DBCC CHECKIDENT (yourtable, reseed, 10000)

?

Shaul
Well this command only resets the identity column seed value for an EXISTING column, does not ADD identity column, read the question carefully.
Binoj Antony