Link to home
Start Free TrialLog in
Avatar of lankapala
lankapala

asked on

Auto number

i have alreday have field called ID(unique data is included), i need to make that as auto number and Primary Key.
i try to use some codes but now work. see below
alter table Emp Alter ID1 int identity(1000, 1);
and i try below code also
ALTER TABLE [Emp] ALTER COLUMN [ID] int identity(1000, 1))
showing error called
Incorrect syntax near the keyword 'identity'.


But its showing error can any one help me to solve above issue.thx
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

You can't alter a column and add identity property for that column.
Instead you can drop the existing column and create new column named ID with Identity property

alter table Emp Drop column ID;
ALTER TABLE [Emp] ADD [ID] int identity(1000, 1)

If you have data loaded into your Emp table, then create a new column named ID_new and then rename it to ID once ID values are autogenerated.
Avatar of lankapala
lankapala

ASKER

After that i try below code
ALTER TABLE [EMP] ALTER COLUMN [ID] INT NOT NULL;
ALTER TABLE [EMP] ADD PRIMARY KEY (ID);
Above code is worked properly

this below code is not working
SET IDENTITY_INSERT [Main] ON

i need to make ID cloumn as a Auto number .(i need to keep previous numbers) and when entering data need to create auto number from last previoud number for example last number is showing in Previous numbers 2301, then i need to start auto number 2302.thx
rrjegan17: i need to keep previous numbers. is it possible to create new auto number after that possible to insert previous numbers to new auto number fields.thx
If you want to start it from 2301 then this would do:

DBCC CHECKIDENT ("EMP", RESEED, 2301);
its showing Error called 'EMP' does not contain an identity column.
don't write this query

ALTER TABLE [Emp] ALTER COLUMN [ID] int identity(1000, 1))

Write this query

ALTER TABLE Emp
ALTER COLUMN ID
ADD CONSTRAINT IDENTITY(1000,1)
Have you created Identity column on EMP table using the statements given earlier.
showing error
samirbhogayta
Incorrect syntax near the keyword 'CONSTRAINT'.
ASKER CERTIFIED SOLUTION
Avatar of rmm2001
rmm2001
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lankapala,

Splitting points with my comments at least as Assisted solution should have been fair..