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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Alter table column for primery key and identity

Author  Topic 

keyursoni85
Posting Yak Master

233 Posts

Posted - 2012-08-10 : 01:58:12
I have table created and want to alter that table using script.
I want to apply primary key and identity(1,1) using script.

I can apply primary key but applying identity gives error. Is anything missing?

ALTER TABLE MyTable ADD PRIMARY KEY (Id)

How can I add identity as well with primary key?

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-08-10 : 03:34:46
1- You cann't ALTER COLUMN for identity
2- Primary Key can added with ALTER TABLE
3- If need to add identity to existing column, copy your data to temp table (or other user table), drop orignal, recreate with identity changes and bring back you data.

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

keyursoni85
Posting Yak Master

233 Posts

Posted - 2012-08-10 : 04:20:29
yes.. got it thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-10 : 10:55:42
quote:
Originally posted by keyursoni85

yes.. got it thanks


see

http://www.mssqltips.com/sqlservertip/1397/add-or-drop-identity-property-for-an-existing-sql-server-column/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -