Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have written a sql query to create autonumber in c#
"select max(id)+1 from table;i
but when i delete 1 record i dont get that id number again with this command.

can any 1 tell me how to this in c#.
Posted
Comments
ashriv 1-Nov-12 8:27am    
why you want to get that deleted id? I think that Id is used as a Primary key so after deleting why you want that again? can you clarify your situation more ...
Sanjay K. Gupta 1-Nov-12 8:40am    
Use Identity for next auto numbering.
For getting the deleted id you can try
select count(id)+1
But it may not be unique.
a1mimo 1-Nov-12 8:43am    
Try google it's faster and easier than waiting for people to answer actually on google there is lots and lots of answers for your problem try search for fill gaps in sql server auto increment

This would only work if you deleted the last record. If you delete an earlier record, that number is gone for good. In general though, it's not a good idea to try to create an id like this. Consider what happens in a multi user system where you have two records both trying to insert into the same table at the same time; both records could end up getting the same ID.
 
Share this answer
 
Thats because its identity column and we did a delete , so whatever number you deleted , you cannot insert a record again for same number.. You need to do identity_insert on and insert record not using max(id)..
 
Share this answer
 
Comments
Pete O'Hanlon 1-Nov-12 11:25am    
There's nowhere in the query that the OP states that it's an identity column. In fact, the logic he's using pretty much tells you that it is not an identity column.

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