Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Alter Table sql 2005

Author  Topic 

lily_wibisana
Starting Member

29 Posts

Posted - 2008-06-13 : 14:40:04
I have imported products table from ms-access to sql server 2005.
So I have already 88 products.
I need to change the Primary key, to auto increment by 1.
I tried this statement,
ALTER TABLE Products ALTER COLUMN ProductsRecordID IDENTITY (89, 1)

But why I got an error
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IDENTITY'.
Much appreciate your help

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-13 : 14:44:02
You can't use ALTER TABLE to add the identity option to an existing column. You must do the long way:

1. Create a new table with the required table layout
2. Move your data, indexes, constraints, etc... to the new table
3. Drop the old table
4. Rename the new table to the old table name

SQL Server Management Studio can do it for you. It will also generate the code for you if you'd like to see the steps or save them.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

lily_wibisana
Starting Member

29 Posts

Posted - 2008-06-13 : 15:18:27
quote:
Originally posted by tkizer

You can't use ALTER TABLE to add the identity option to an existing column. You must do the long way:

1. Create a new table with the required table layout
2. Move your data, indexes, constraints, etc... to the new table
3. Drop the old table
4. Rename the new table to the old table name

SQL Server Management Studio can do it for you. It will also generate the code for you if you'd like to see the steps or save them.

Tara Kizer


How do I move the data to the new table?
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-13 : 15:21:32
INSERT INTO/SELECT

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-14 : 02:18:41
http://www.mssqltips.com/tip.asp?tip=1397
Go to Top of Page
   

- Advertisement -