Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have 10 records in my table. i want to modify the table column with identity

like,

ALTER TABLE ALTER COLUMN INT IDENTITY (10, 1)
Already 10 Values are inserted in Table.

My Error:
Incorrect syntax near the keyword 'IDENTITY'.
Posted

SQL
Alter Table Names
Add Id_new Int Identity(10, 1)
Go

Alter Table Names Drop Column ID
Go

Exec sp_rename 'Names.Id_new', 'ID', 'Column'


Please check.
http://stackoverflow.com/questions/1049210/adding-an-identity-to-an-existing-column[^]
 
Share this answer
 
Comments
oliver grace 26-Jul-12 4:31am    
Thank you for your reply ..in this approach i can't retain the existing data..
when i alter a table i want to have my existing data also...is there anyother way.....
Santhosh Kumar Jayaraman 26-Jul-12 4:33am    
try this.

n this Approach you can retain the existing data values on the newly created identity column.

CREATE TABLE dbo.Tmp_Names
(
Id int NOT NULL
IDENTITY(1, 1),
Name varchar(50) NULL
)
ON [PRIMARY]
go

SET IDENTITY_INSERT dbo.Tmp_Names ON
go

IF EXISTS ( SELECT *
FROM dbo.Names )
INSERT INTO dbo.Tmp_Names ( Id, Name )
SELECT Id,
Name
FROM dbo.Names TABLOCKX
go

SET IDENTITY_INSERT dbo.Tmp_Names OFF
go

DROP TABLE dbo.Names
go

Exec sp_rename 'Tmp_Names', 'Names'
oliver grace 26-Jul-12 4:45am    
Where is the dbo.Names table are you already created this table
Santhosh Kumar Jayaraman 26-Jul-12 4:48am    
Yes DBO.Names is the existing table. Here we are creating new table with identity column and insert existing records into new table and dropping the old table.
oliver grace 26-Jul-12 5:13am    
Thank you Very mach... i got the answer
You forgot the column name - it should be between the words "COLUMN" and "INT"
 
Share this answer
 
Remove the old column and add new one.
SQL
--SQL Script:
ALTER TABLE Users DROP PK_Users
GO
ALTER TABLE Users DROP COLUMN UserID
GO
ALTER TABLE Users ADD UserID int NOT NULL IDENTITY(1,1) 
GO
ALTER TABLE Users ADD CONSTRAINT PK_Users PRIMARY KEY CLUSTERED ([UserID] ASC)
GO
 
Share this answer
 
Comments
oliver grace 26-Jul-12 4:35am    
Thank you...But my requirement is when i alter a table old column data also i need....is there anyother way.
In case if user wants identity also in varchar column for the above scenario..


SQL
drop table testtable2
go
CREATE TABLE TestTable2 (
 srlNo int NOT NULL IDENTITY(1, 1),
  Id varchar(100),
   Name varchar(50) NULL,
    Addres varchar(100) 
    ) ON [PRIMARY] 
    go 
    
   insert into TestTable2 values('test1','sant','test')
   go
    CREATE TABLE TempTable2 ( 
    Identifier int NOT NULL IDENTITY(1, 1),
     Id as 'test'+cast(Identifier as varchar(100)),
      Name varchar(50) NULL,
       Addres varchar(100)
       ) ON [PRIMARY] 
       go 
       SET IDENTITY_INSERT TempTable2 ON
        go 
        IF EXISTS ( SELECT * FROM TestTable2 ) 
        INSERT INTO TempTable2 (Identifier, Name,Addres ) 
        SELECT srlNo,Name, Addres FROM TestTable2 
        TABLOCKX 
        go 
        SET IDENTITY_INSERT TempTable2 OFF 
        go
        DROP TABLE TestTable2
         go
          Exec sp_rename 'TempTable2', 'TestTable2'
          
          select * from testtable2
 
Share this answer
 
v2

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