Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi everybodyy,

I have a table in sql server 2008 I need to alter it to add a AutoIncrement for already existing primary key field in this table.

Please I need the script to execute this query.

Can anybody help?

thank you
Posted

If you have ms sql srvr mgmt studio you can do it. You just have to do it manually, not in script or command form.



connect to your db

show the table/column names in object explorer window

right click on column name

select modify

in column properties tab

alter "identity specification"/"is identity" to YES

set increment/seed properties as desired


You can't alter the existing columns for identity through query



You have 2 options,

1. Create a new table with identity & drop the existing table

2. Create a new column with identity & drop the existing column

check this

http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/04d69ee6-d4f5-4f8f-a115-d89f7bcbc032[^]
 
Share this answer
 
 
Share this answer
 
SQL
CREATE TABLE Student
 (
  Id        INT NOT NULL,
  Name    VARCHAR(100) NOT NULL
  )
go
/*
Populating Customer table,
using same TEST_Sequence to generate the Id column,
which we used for Cutomer table in past
*/
INSERT Student(Id, Name)
VALUES
(NEXT VALUE FOR TEST_Sequence, 'Ram'),
(NEXT VALUE FOR TEST_Sequence, 'Rita'),
(NEXT VALUE FOR TEST_Sequence, 'Ron')
go
select * from Student


by this code may help you....
thanku
 
Share this answer
 
v2
Comments
nourbt 13-Oct-11 4:52am    
thank you anyway, but what i needed is to make this field autoincrement in its properties
i have one city table i copy city table to temp table sd and add column sid auto increment
SQL
select name into #sd from city
ALTER TABLE #sd
ADD sid int NOT NULL IDENTITY (1,1) PRIMARY KEY
select * from #sd
drop table #sd


other solution
SQL
ALTER TABLE city
ADD cid int NOT NULL IDENTITY (1,1) PRIMARY KEY
 
Share this answer
 
v2
Comments
CHill60 27-Dec-13 13:41pm    
Reason for my downvote ... your first solution doesn't actually work and your 2nd doesn't solve the OP's problem of already having a primary key in place. That and the question is 2 years old and already adequately answered

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