Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.20/5 (3 votes)
See more:
alter column id int not null Identity(1,1)..

Its giving me an error as Incorrect syntax near the keyword 'Identity'.
Posted
Updated 19-Apr-17 22:55pm

first run this

SQL
Alter TABLE [dbo].[t_name]
    drop column [id]


then run this
SQL
Alter TABLE [dbo].[t_name]

add  [id] [int] IDENTITY(1,1) NOT NULL
 
Share this answer
 
Normally, I use the SQL Server Management Studio. I can simply toggle IDENTITY on and off. Lacking this, one solution is to add a new column and then drop your old one.

SQL
ALTER TABLE (yourTable) ADD NewColumn INT IDENTITY(1,1)

ALTER TABLE (yourTable) DROP COLUMN OldColumnName



.
 
Share this answer
 
v2
Comments
Vishal.Singh879 14-Aug-14 8:23am    
i have to add identity to my existing column
 
Share this answer
 
Hi,

Check this...


How to alter column to identity[^]


Hope this will help you.

Cheers
 
Share this answer
 
Hi Vishal

try this

SQL
alter table t_name
alter  id int identity(1,1)



Thanks
Mangesh
 
Share this answer
 
v2
Comments
Vishal.Singh879 14-Aug-14 8:22am    
Its not working
There are potential methods like below of solving this problem.

1. Drop and re-create table with INT IDENTITY column
2. Drop INT column and re-create it as an INT IDENTITY column
3. ALTER column with INT IDENTITY NOT NULL (only if there is no NULL values in it already eg. clean table)
4. Add new INT IDENTITY column to the table next to INT column and use such new column then.
 
Share this answer
 
Comments
CHill60 20-Apr-17 5:30am    
The question is over 2 years old and already had 5 solutions. All of your suggestions have already been covered, with the correct syntax.
Posting solutions to old questions can quickly lead to loss of reputation points and in extreme cases can lead to your account being suspended. Be very wary of doing it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900