Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
PERSON table have 115 records here.
id |name
1 |aaa
2 |bbb
3 |ccc

.
.
.
115|abv

i need to alter PERSON table for column ID identity.

how to change ...
like this way.

SQL
alter table [PERSON]
alter column ID bigint not null Identity(1,1)


but i get this error
Incorrect syntax near the keyword 'Identity'.

Regards,
Karthikeyan,
Bangalore.
Posted
Comments
pkarthionline 4-Oct-12 10:06am    
This way also got error.

alter table [Person]
alter column ID bigint not null Identity(116,1)

Incorrect syntax near the keyword 'Identity'.
n.podbielski 4-Oct-12 10:38am    
Because identity cannot be set at alter column
http://msdn.microsoft.com/pl-pl/library/ms190273.aspx

Try this:
DBCC CHECKIDENT ("tablename", RESEED, 10);
 
Share this answer
 
Comments
Tejas Vaishnav 5-Oct-12 8:21am    
It will work when there is already an identity column and reset its value to our specific value, in your case it will be 10.
dimpledevani 5-Oct-12 8:42am    
well sorry but as far as i understood the question i thought he needed to reset the value so i gave this solution
please modified this script as per your requirement to add identity column to you existing table.

SQL
/* To prevent any potential data loss issues, you should review this script in detail 
before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_PERSON
	(
	ID int NOT NULL IDENTITY (1, 1),
	NAME varchar(25) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_PERSON SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_PERSON ON
GO
IF EXISTS(SELECT * FROM dbo.PERSON)
	 EXEC('INSERT INTO dbo.Tmp_PERSON (ID, NAME)
		SELECT ID, NAME FROM dbo.PERSON WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_PERSON OFF
GO
DROP TABLE dbo.PERSON
GO
EXECUTE sp_rename N'dbo.Tmp_PERSON', N'PERSON', 'OBJECT' 
GO
COMMIT


for more information please review this link
SQL Server Add Remove Identity[^]
OR
Sql Server Add Remove Identity 2[^]
 
Share this answer
 
v2
Comments
gvprabu 7-Oct-13 11:51am    
nice example

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