Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have one question about sql when we delete one row suppose 102 and then if i insert then that inserted record should be 102 so i want to reset my identity each time ? help me......
Posted
Comments
Bernhard Hiller 1-Aug-13 2:28am    
Waht is the reason for that requirement?
I'd prefer to have the database engine handling the ID values: that's just something internal to the mode of working of a relational database, and nothing to be shown to the user. Just imagine a different type of storage which does not allow you to see its internal identification methods at all, handling it automagically for you...
Dholakiya Ankit 1-Aug-13 2:33am    
nice hiller thanks fr advice

Hi Ankit,

You can create one function which will accept the name of the table as a parameter.

The functionality of that table is to identify the missing id.

For Example :
In my table 'Test' i have Ids : 1,2,3,5,6

Now when I call that function MyFunction('Test'), this function should return 4 as an identity.

And you can use that returned id.

ForExample :

INSERT INTO
Test
(Id,FirstName,LastName)
VALUES
(MyFunction('Test'),'aaa','bbb')
 
Share this answer
 
Comments
Dholakiya Ankit 1-Aug-13 2:13am    
yes good one but should be sequence 1,2,3,4,5,6 with out function would be great
CodeBlack 1-Aug-13 2:18am    
As per my knowledge, I dont think that without using any function we can achieve this.
Bernhard Hiller 1-Aug-13 2:25am    
Dangerous: what about two clients trying to insert a new row at the same time?
CodeBlack 1-Aug-13 2:43am    
simple, set the identity of the column to true :) Sql itself handles the identity and two requests come at same time than thread will be handled :)
Dholakiya Ankit 1-Aug-13 2:33am    
okay get it
after delete the row from table, you have to reset the identity of the table as

DBCC CHECKIDENT (yourtable_name, reseed, 101)

then your next record inserted with an identity 102

http://www.dotnetbysatya.co.nr
 
Share this answer
 
Comments
Dholakiya Ankit 1-Aug-13 2:14am    
nice one
i accept that its not good idea to reset identity.......::)
 
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