?
Solved

Auto number

Posted on 2010-11-20
10
Medium Priority
?
724 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:lankapala
10 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34179427
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.
0
 
LVL 2

Author Comment

by:lankapala
ID: 34179428
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
0
 
LVL 2

Author Comment

by:lankapala
ID: 34179435
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
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34179485
If you want to start it from 2301 then this would do:

DBCC CHECKIDENT ("EMP", RESEED, 2301);
0
 
LVL 2

Author Comment

by:lankapala
ID: 34179540
its showing Error called 'EMP' does not contain an identity column.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34179545
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)
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34179664
Have you created Identity column on EMP table using the statements given earlier.
0
 
LVL 2

Author Comment

by:lankapala
ID: 34180254
showing error
samirbhogayta
Incorrect syntax near the keyword 'CONSTRAINT'.
0
 
LVL 7

Accepted Solution

by:
rmm2001 earned 2000 total points
ID: 34180379
Go with what @rrjegan17 was saying with DBCC CHECKIDENT ("EMP", RESEED, 2301) if you want your keys to start at 2301 after you run this script.

Easiest way to do this though is going to be to recreate the table of sorts to maintain what you already have. Put the current rows that you have into a temp table with the identity (1000, 1) like you wanted, but with identity_insert on so that you can keep the ids already there. Then truncate out your original table. Then modify your ID column. Then identity_insert back into the original table with the new constraints. Remember to always backup your tables before you change any constraints like this in case something goes awol.
CREATE #Table1 ( ID INT IDENTITY (1000, 1), Field1 VARCHAR(50), Field2 VARCHAR(50) )

SET IDENTITY_INSERT #Table1 ON
INSERT #Table1
SELECT ID, Field1, Field2 FROM <YourTable>
SET IDENTITY_INSERT #Table1 OFF

TRUNCATE TABLE <YourTable>

ALTER TABLE <YourTable> DROP COLUMN ID
ALTER TABLE <YourTable> ADD ID INT IDENTITY(1000, 1)

SET IDENTITY_INSERT <YourTable> ON
INSERT <YourTable> (ID, Field1, Field2)
SELECT ID, Field1, Field2
FROM #Table1
SET IDENTITY_INSERT <YourTable> OFF

DROP #Table1

Open in new window

0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34288403
lankapala,

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question