Click here to Skip to main content
12,506,338 members (55,349 online)
Rate this:
 
Please Sign up or sign in to vote.
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.

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 4-Oct-12 3:46am
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

please modified this script as per your requirement to add identity column to you existing table.

/* 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[^]
  Permalink  
v2
Comments
gvprabu 7-Oct-13 11:51am
   
nice example
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try this:
DBCC CHECKIDENT ("tablename", RESEED, 10);
  Permalink  
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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160927.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100