Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hi all,

I have a table as emp(empid primary key,identity,sal,dept).
I inserted 10 values like
empid  sal dept

1     2000  3
2     3000  2
3     500   1
.     ...   .
.     ...   .
.     ...   .
10    2000  3


Now I deleted empid from 5 to 10.

Now I am inserting new values it's empid takes from 11.....
but here will need to takes empid id as 6........

How can I over come this .........please any body help to me
Thanks in advance..........
Posted
Updated 31-May-12 0:06am
v3
Comments
Arunprasath Natarajan 31-May-12 6:12am    
If ID is Auto generated you can not get the deleted ID.

If SET IDENTITY_INSERT is ON then if you try to delete the middle rows data and again if you try to add the new row it will take max(id)+1 for the new row not the middle rows values.

Inorder to insert middle rows manually you have to do it with by setting SET IDENTITY_INSERT to off.

SET IDENTITY_INSERT OFF
insert into emp(empid primary key,identity,sal,dept) values (6, your values).

then again try to on the IDENTITY_INSERT. then it will continue with taking id as
max(id)+1.

SET IDENTITY_INSERT ON

<pre lang="SQL">
SET IDENTITY_INSERT OFF
insert into emp(empid primary key,identity,sal,dept) values (6, your values).
SET IDENTITY_INSERT ON
 
Share this answer
 
Comments
Unareshraju 1-Jun-12 0:35am    
THANK U SOMUCH ASHOK.................WORKING FINE
Hi You cannot get deleted id. because ur primary key will be auto generated.

if you want to insert record with deleted id then you have to

SQL
SET IDENTITY_INSERT OFF
insert into emp(empid primary key,identity,sal,dept) values (6, your values).
SET IDENTITY_INSERT ON
 
Share this answer
 
v2
Hi,

Try read this article:


[SQL-How-to-find-holes-in-sequences]

Using the last sql to get the first free number and then use this number in the insert query. Remember to use the IDENTITY_INSERT when inserting values into an identity column.

Regards
Joachim
 
Share this answer
 
Hi u cannot get deleted id. because ur primary key will be auto generated.

if u remove auto generated and identty is yes to no u will get it.
 
Share this answer
 

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